Tell me more ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I need some useful functions that faster than the original. (Original - C++'s functions)

For Example:

#include <time.h>
#include <stdio.h>
#include <Windows.h>

struct TestMemory {
    double a,b,c;
};

void* __TestMemory__Sample=0;

int WINAPI WinMain(HINSTANCE h1,HINSTANCE h2,LPSTR str,int i) {
    AllocConsole();
    __TestMemory__Sample=calloc(1,sizeof(TestMemory));
    TestMemory* test=(TestMemory*)malloc(sizeof(TestMemory));
    clock_t c1=clock(),c2;
    for (int i=0;i<100000000;i++)
       memcpy(test,__TestMemory__Sample,sizeof(TestMemory));
    c1=clock()-c1;
    c2=clock();
    for (int i=0;i<100000000;i++)
       memset(test,0,sizeof(TestMemory));
    c2=clock()-c2;
    printf("memcpy: %d\nmemset: %d\nradio: %f (set/cpy)",c1,c2,c2/(float)c1);
    getchar();
}

Output:

memcpy: 1410
memset: 3250
radio: 2.304965 (set/cpy)

This function replace the current way to zero memory.

What it does: Create global pointer ,first set in it zeroed memory. After that ,when you want to allocate zeroed memory. Allocate memory with malloc and then copy the zeroed memory (Global variable) to this memory.

Note: If I cant ask this thing here then delete it. (Someone navigated me here)

share|improve this question
1  
1) Did you turn on the optimizer. 2) Did you check to see if your code has been removed by the optimizer. 3) Check that you are timing what you think you are timming. – Loki Astari Jul 31 '12 at 21:20
I know about all of those ,but I need more then this. I am programming scripting language. I need to make it fast as native (C++). – MessyCode Jul 31 '12 at 21:23
1  
As expected I get the exact opposite: memcpy: 141861 memset: 139421 radio: 0.982800 – Loki Astari Jul 31 '12 at 22:28
That's when you enabling optimizations ,it's deleting all the code. Use it in variables that you really use. – MessyCode Aug 1 '12 at 13:54
Sorry ,but I need this. – MessyCode Aug 18 '12 at 22:35
show 1 more comment

1 Answer

Beware of optimising things, trying to get tiny improvements. By running your program with varying optimisation settings, I got the following ratios (memcpy/memset):

  • 1.8
  • 1.0
  • 0.8
  • 5.1

I think you can use these to prove anything you like. If your program is running too slowly, your attention probably should be directed elsewhere.

share|improve this answer
That's when you enabling optimizations ,it's deleting all the code. Use it in variables that you really use. – MessyCode Aug 1 '12 at 13:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.