Not necessarily. Of the three methods I've tested, Blah()
appears to be the slowest.
The method:
Blah poo;
memset(&poo, 0, sizeof(Blah));
is the fastest, and
Blah poo = {0}
is a very close second;
Basically, you must compromise speed for safety and portability.
This is first time I've ever timed functions. I'm Visual Studio.NET
2003, SDK, WindowsXP.
My timing algorithm:
LARGE_INTEGER start;
LARGE_INTEGER end;
QueryPerformanceCounter( &start );
...Do Something
QueryPerformanceCounter( &end );
TRACE( TEXT( "Time = %d\n" ), end.QuadPart-start.QuadPart );
Granted, we're talking minimal differences, but that's besides the
point.
Please explain a little deeper as to why memset might not set a
pointer or a floating point number to zero. It does on my computer.
-Nollie
Sometimes it takes me 10mins 23secs to get to the bustop. Sometimes it takes
9mins 58secs...
I wouldn't use "QueryPerformanceCounter" as an indicator for this, unless
you time it a few thousand times and get the average, but even still, I'm
not sure how accurate it'd be.
As regards pointers... well let's say that on a certain system, a pointer
variable takes up 32 bits in memory, as so:
0000 0000 0000 0000 0000 0000 0000 0000
What you're looking at above is "all bits zero". On Windows, this indicates
that the pointer is a null pointer. Now imagine a system where the memory
address 0 is a valid one, ie. it's the first byte of memory, and that on
this particular system, the null pointer value is:
1111 1111 1111 1111 1111 1111 1111 1111
When you write a program with the following line in it:
int* p_k = 0;
The compiler doesn't produce code that sets all bits to zero... no no...
what it does is produce code that sets it to the null pointer value for that
system (and/or for that type, I believe systems may choose to have different
null pointer values depending on the type...). But "memset" doesn't have a
clue about this, all it does is set all bits to zero, which may be a valid
memory address on some systems, hence it's not portable.
And as regards floating point numbers, implementations aren't obligated to
represent the value zero as "all bits zero" either.
I don't see how either of the three could be slower/faster than each other,
they should all yield the same machine code (except maybe the call to
"memeset" might add overhead if it's not inline...)
-JKop