struct vs class benchmark (was Re: Give struct the status it
Dave
Dave_member at pathlink.com
Mon Mar 27 07:16:38 PST 2006
In article <17vpw8ovosqie$.1f65d29ys83k6$.dlg at 40tude.net>, Derek Parnell says...
>
>On Mon, 27 Mar 2006 04:11:37 +0000 (UTC), Dave wrote:
>
>> These run as the same speed on my system, which is pretty cool since the
>> accessor function for class Point.x is vitual. If you finalize it the class
>> version is actually faster. No doubt the OP has a point when you try to alloc.
>> classes in a tight loop, and I'd agree that stack allocated classes would be
>> great, but if you can work around the allocation bottleneck then classes tend to
>> be faster and easier to code because they are passed around by reference w/o
>> pointer syntax.
>
>I wasn't so sure about the 'passed around' claim. So I amended my test to
>pass the object and the results were almost identical. So it appears that
>passing structs is not an issue at all. In fact, if you look at the
>generated machine code, the struct seems to be passed by reference even if
>you code otherwise.
>
Not what I'm seeing - give this a try:
import std.stdio, std.date;
struct S
{
char[1000] str;
}
class C
{
char[1000] str;
}
void main()
{
const int y = 1_000_000;
{
S s;
d_time st = getUTCtime();
for(int x = 0; x < y; x++)
{
fooS(s);
}
d_time en = getUTCtime();
writefln((en-st) / cast(double)TicksPerSecond);
}
{
C c = new C;
d_time st = getUTCtime();
for(int x = 0; x < y; x++)
{
fooC(c);
}
d_time en = getUTCtime();
writefln((en-st) / cast(double)TicksPerSecond);
}
}
void fooS(S s)
{
s.str[] = '\0';
}
void fooC(C c)
{
c.str[] = '\0';
}
More information about the Digitalmars-d
mailing list