Unexpected performance
-
Any ideas why the following code runs faster (19 vs 37 msec in the first test) with
/clr
flag on? No clr is used in the code at all!#include "stdafx.h"
class myClass
{
};int main()
{
clock_t start;
const int COUNT = 100000;
myClass* objects[COUNT];
start = clock();
for (int i = 0; i < COUNT; i++) {
objects[i] = new myClass();
delete objects[i];
}
printf("Native with delete: %f\n", (double)(clock() - start));start = clock(); for (int i = 0; i < COUNT; i++){ objects\[i\] = new myClass(); } clock\_t start2 = clock(); for (int i = 0; i < COUNT; i++){ delete objects\[i\]; } printf("Native without delete: %f\\n", (double)(start2 - start)); printf("Deletion: %f\\n", (double)(clock() - start2)); printf("Total: %f\\n", (double)(clock() - start));
}
Greetings - Gajatko Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
-
Any ideas why the following code runs faster (19 vs 37 msec in the first test) with
/clr
flag on? No clr is used in the code at all!#include "stdafx.h"
class myClass
{
};int main()
{
clock_t start;
const int COUNT = 100000;
myClass* objects[COUNT];
start = clock();
for (int i = 0; i < COUNT; i++) {
objects[i] = new myClass();
delete objects[i];
}
printf("Native with delete: %f\n", (double)(clock() - start));start = clock(); for (int i = 0; i < COUNT; i++){ objects\[i\] = new myClass(); } clock\_t start2 = clock(); for (int i = 0; i < COUNT; i++){ delete objects\[i\]; } printf("Native without delete: %f\\n", (double)(start2 - start)); printf("Deletion: %f\\n", (double)(clock() - start2)); printf("Total: %f\\n", (double)(clock() - start));
}
Greetings - Gajatko Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
Because managed code ROCKS! But seriously...
gajatko wrote:
19 vs 37 msec
It's unlikely your tick count has a resolution better that 19ms. That looks suspiciously like the difference between a single tick. Maybe try timing something that takes a few seconds or use the performance counter for timing. Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Because managed code ROCKS! But seriously...
gajatko wrote:
19 vs 37 msec
It's unlikely your tick count has a resolution better that 19ms. That looks suspiciously like the difference between a single tick. Maybe try timing something that takes a few seconds or use the performance counter for timing. Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
With
const int COUNT = 10000000;
myClass** objects = new myClass*[COUNT];(heap because of stackoverflow) I got 3582 native and 1612 with /clr
Greetings - Gajatko Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
-
With
const int COUNT = 10000000;
myClass** objects = new myClass*[COUNT];(heap because of stackoverflow) I got 3582 native and 1612 with /clr
Greetings - Gajatko Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
gajatko wrote:
I got 3582 native and 1612 with /clr
Which numbers are you reporting there? Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
gajatko wrote:
I got 3582 native and 1612 with /clr
Which numbers are you reporting there? Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
start = clock();
for (int i = 0; i < COUNT; i++) {
objects[i] = new myClass();
delete objects[i];
}
printf("Native with delete: %f\n", (double)(clock() - start) / CLOCKS_PER_SEC * 1000);Greetings - Gajatko Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
-
start = clock();
for (int i = 0; i < COUNT; i++) {
objects[i] = new myClass();
delete objects[i];
}
printf("Native with delete: %f\n", (double)(clock() - start) / CLOCKS_PER_SEC * 1000);Greetings - Gajatko Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
Try this (and do NOT run in the debugger!)
class myClass
{
};int _tmain(int argc, _TCHAR* argv[])
{
const int COUNT = 1000000;
myClass** objects = new myClass*[COUNT];clock\_t start1 = clock(); for (int i = 0; i < COUNT; i++) { objects\[i\] = new myClass(); delete objects\[i\]; } clock\_t end1 = clock(); clock\_t start2 = clock(); for (int i = 0; i < COUNT; i++){ objects\[i\] = new myClass(); } clock\_t end2 = clock(); clock\_t start3 = clock(); for (int i = 0; i < COUNT; i++){ delete objects\[i\]; } clock\_t end3 = clock(); printf("Loop with new/delete: %d\\n\\n", end1 - start1); printf("Loop with new: %d\\n", end2 - start2); printf("Loop with delete: %d\\n", end3 - start3); printf("Total of loop new and loop delete: %d\\n", end3 - start2); printf("\\n\\nPress enter to end..."); getchar(); delete\[\] objects; return 0;
}
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Try this (and do NOT run in the debugger!)
class myClass
{
};int _tmain(int argc, _TCHAR* argv[])
{
const int COUNT = 1000000;
myClass** objects = new myClass*[COUNT];clock\_t start1 = clock(); for (int i = 0; i < COUNT; i++) { objects\[i\] = new myClass(); delete objects\[i\]; } clock\_t end1 = clock(); clock\_t start2 = clock(); for (int i = 0; i < COUNT; i++){ objects\[i\] = new myClass(); } clock\_t end2 = clock(); clock\_t start3 = clock(); for (int i = 0; i < COUNT; i++){ delete objects\[i\]; } clock\_t end3 = clock(); printf("Loop with new/delete: %d\\n\\n", end1 - start1); printf("Loop with new: %d\\n", end2 - start2); printf("Loop with delete: %d\\n", end3 - start3); printf("Total of loop new and loop delete: %d\\n", end3 - start2); printf("\\n\\nPress enter to end..."); getchar(); delete\[\] objects; return 0;
}
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Mark Salsbery wrote:
Try this (and do NOT run in the debugger!)
Did you look something like this[^] while typing that in?
led mike
LMAO! You betcha! :laugh:
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Try this (and do NOT run in the debugger!)
class myClass
{
};int _tmain(int argc, _TCHAR* argv[])
{
const int COUNT = 1000000;
myClass** objects = new myClass*[COUNT];clock\_t start1 = clock(); for (int i = 0; i < COUNT; i++) { objects\[i\] = new myClass(); delete objects\[i\]; } clock\_t end1 = clock(); clock\_t start2 = clock(); for (int i = 0; i < COUNT; i++){ objects\[i\] = new myClass(); } clock\_t end2 = clock(); clock\_t start3 = clock(); for (int i = 0; i < COUNT; i++){ delete objects\[i\]; } clock\_t end3 = clock(); printf("Loop with new/delete: %d\\n\\n", end1 - start1); printf("Loop with new: %d\\n", end2 - start2); printf("Loop with delete: %d\\n", end3 - start3); printf("Total of loop new and loop delete: %d\\n", end3 - start2); printf("\\n\\nPress enter to end..."); getchar(); delete\[\] objects; return 0;
}
Mark Salsbery Microsoft MVP - Visual C++ :java:
OK so what should I see? I get results: For /clr:
Loop with new/delete: 174
Loop with new: 101
Loop with delete: 77
Total of loop new and loop delete: 178Press enter to end...
for not /clr:
Loop with new/delete: 306
Loop with new: 90
Loop with delete: 64
Total of loop new and loop delete: 154Press enter to end...
And I neither run from IDE nor in Debug mode. According to a "you pay as you go" C++ philosophy the result should be the same.
Greetings - Gajatko Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
-
Mark Salsbery wrote:
Try this (and do NOT run in the debugger!)
Did you look something like this[^] while typing that in?
led mike
Good one! Personally I get used to strange things happening with M$ implementation of Java VM*, so I look like this constantly when programming. Uhm something wrong with logic is here but who cares. Cheers :-D * - ok, just kidding.
Greetings - Gajatko Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
-
Any ideas why the following code runs faster (19 vs 37 msec in the first test) with
/clr
flag on? No clr is used in the code at all!#include "stdafx.h"
class myClass
{
};int main()
{
clock_t start;
const int COUNT = 100000;
myClass* objects[COUNT];
start = clock();
for (int i = 0; i < COUNT; i++) {
objects[i] = new myClass();
delete objects[i];
}
printf("Native with delete: %f\n", (double)(clock() - start));start = clock(); for (int i = 0; i < COUNT; i++){ objects\[i\] = new myClass(); } clock\_t start2 = clock(); for (int i = 0; i < COUNT; i++){ delete objects\[i\]; } printf("Native without delete: %f\\n", (double)(start2 - start)); printf("Deletion: %f\\n", (double)(clock() - start2)); printf("Total: %f\\n", (double)(clock() - start));
}
Greetings - Gajatko Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
I'm not sure what you're expecting here. First, if you were running tests in the debugger, forgetaboutit. Second, comparing times for two sets of loops isn't going to work. Try putting the loop with the new/delete combined below the other two loops and you'll see what I mean. There's alot going on with the heap there, making the results...well, there's no comparison. Third, comparing native to CLR build...the MSIL is better optimized than the debug native code (assuming that's what you were comparing to). Again, no comparison. I would guess, if you figure out the right combination of optimization switches, you MAY be able to make the native version a TINY bit faster than the managed version...maybe... Lastly, don't believe people that say managed code is slower :)
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
OK so what should I see? I get results: For /clr:
Loop with new/delete: 174
Loop with new: 101
Loop with delete: 77
Total of loop new and loop delete: 178Press enter to end...
for not /clr:
Loop with new/delete: 306
Loop with new: 90
Loop with delete: 64
Total of loop new and loop delete: 154Press enter to end...
And I neither run from IDE nor in Debug mode. According to a "you pay as you go" C++ philosophy the result should be the same.
Greetings - Gajatko Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
OK so what should I see? I get results: For /clr:
Loop with new/delete: 174
Loop with new: 101
Loop with delete: 77
Total of loop new and loop delete: 178Press enter to end...
for not /clr:
Loop with new/delete: 306
Loop with new: 90
Loop with delete: 64
Total of loop new and loop delete: 154Press enter to end...
And I neither run from IDE nor in Debug mode. According to a "you pay as you go" C++ philosophy the result should be the same.
Greetings - Gajatko Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
By the way, just curious - what's your test platform (CPU(s)/speed) and compiler version?
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
By the way, just curious - what's your test platform (CPU(s)/speed) and compiler version?
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Good one! Personally I get used to strange things happening with M$ implementation of Java VM*, so I look like this constantly when programming. Uhm something wrong with logic is here but who cares. Cheers :-D * - ok, just kidding.
Greetings - Gajatko Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
-
By the way, just curious - what's your test platform (CPU(s)/speed) and compiler version?
Mark Salsbery Microsoft MVP - Visual C++ :java:
Vista B. x64 SP 1 Intel Quad 2.4, 2 GB RAM, VS 2008
Greetings - Gajatko Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
-
I'm not sure what you're expecting here. First, if you were running tests in the debugger, forgetaboutit. Second, comparing times for two sets of loops isn't going to work. Try putting the loop with the new/delete combined below the other two loops and you'll see what I mean. There's alot going on with the heap there, making the results...well, there's no comparison. Third, comparing native to CLR build...the MSIL is better optimized than the debug native code (assuming that's what you were comparing to). Again, no comparison. I would guess, if you figure out the right combination of optimization switches, you MAY be able to make the native version a TINY bit faster than the managed version...maybe... Lastly, don't believe people that say managed code is slower :)
Mark Salsbery Microsoft MVP - Visual C++ :java:
Mark Salsbery wrote:
I'm not sure what you're expecting here.
Not sure what to expect, but definately not this (sounds good).
Mark Salsbery wrote:
First, if you were running tests in the debugger, forgetaboutit.
Hey, I'm not an urgent-codes-plz-gimme-guy and I do not do tests in a debugger!!
Mark Salsbery wrote:
Second, comparing times for two sets of loops isn't going to work.
That was secondary tests. In fact, the first one was important to me (ok, not important. All this talk is neither interesting nor important, but I must figure the thing out).
Mark Salsbery wrote:
the MSIL is better optimized
What could be optimized in such a simple code?
Mark Salsbery wrote:
Lastly, don't believe people that say managed code is slower
Lucky and happy you. The problem is that if you believe something (God, Holy Grail, etc.) you likely believe people as well. Oh sorry I'm getting too soap-boxy.
Greetings - Gajatko Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
-
Mark Salsbery wrote:
I'm not sure what you're expecting here.
Not sure what to expect, but definately not this (sounds good).
Mark Salsbery wrote:
First, if you were running tests in the debugger, forgetaboutit.
Hey, I'm not an urgent-codes-plz-gimme-guy and I do not do tests in a debugger!!
Mark Salsbery wrote:
Second, comparing times for two sets of loops isn't going to work.
That was secondary tests. In fact, the first one was important to me (ok, not important. All this talk is neither interesting nor important, but I must figure the thing out).
Mark Salsbery wrote:
the MSIL is better optimized
What could be optimized in such a simple code?
Mark Salsbery wrote:
Lastly, don't believe people that say managed code is slower
Lucky and happy you. The problem is that if you believe something (God, Holy Grail, etc.) you likely believe people as well. Oh sorry I'm getting too soap-boxy.
Greetings - Gajatko Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.
gajatko wrote:
Not sure what to expect, but definately not this
Definitely didn't expect what?
gajatko wrote:
Lucky and happy you.
I have no idea what you mean by that. I didn't state anything about me.
gajatko wrote:
The problem is that if you believe something (God, Holy Grail, etc.) you likely believe people as well.
Not sure what that means either. Just because I believe I'll have another beer doesn't mean I believe people.
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
gajatko wrote:
Not sure what to expect, but definately not this
Definitely didn't expect what?
gajatko wrote:
Lucky and happy you.
I have no idea what you mean by that. I didn't state anything about me.
gajatko wrote:
The problem is that if you believe something (God, Holy Grail, etc.) you likely believe people as well.
Not sure what that means either. Just because I believe I'll have another beer doesn't mean I believe people.
Mark Salsbery Microsoft MVP - Visual C++ :java:
It looks like I reached a maximum humour abstraction level last night. Do not take that serious please. :-O :rolleyes:
Greetings - Gajatko Portable.NET is part of DotGNU, a project to build a complete Free Software replacement for .NET - a system that truly belongs to the developers.