-2
\$\begingroup\$

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:

  1. Create a global pointer, first set with zeroed memory.
  2. When you want to allocate zeroed memory, allocate memory with malloc and then copy the zeroed memory (global variable) to this memory.
\$\endgroup\$
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ – Martin York Jul 31 '12 at 21:20
  • \$\begingroup\$ 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++). \$\endgroup\$ – MessyCode Jul 31 '12 at 21:23
  • 1
    \$\begingroup\$ As expected I get the exact opposite: memcpy: 141861 memset: 139421 radio: 0.982800 \$\endgroup\$ – Martin York Jul 31 '12 at 22:28
  • \$\begingroup\$ That's when you enabling optimizations ,it's deleting all the code. Use it in variables that you really use. \$\endgroup\$ – MessyCode Aug 1 '12 at 13:54
  • 1
    \$\begingroup\$ Need what? You have an idea, which may or may not be faster than just using memset under some circumstances. Is that what you need? Or you need it to actually be faster? \$\endgroup\$ – Useless Aug 31 '12 at 12:36
1
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ That's when you enabling optimizations ,it's deleting all the code. Use it in variables that you really use. \$\endgroup\$ – MessyCode Aug 1 '12 at 13:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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