# Zeroing memory on Windows

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;
getchar();
}


Output:

memcpy: 1410
memset: 3250


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.
• 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. – Martin York 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
• As expected I get the exact opposite: memcpy: 141861 memset: 139421 radio: 0.982800 – Martin York 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
• 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? – Useless Aug 31 '12 at 12:36