Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
I have made two very basic programs for the purpose of checking the difference in speed between C# and C++. The results where not that different, but something amazing is happening that I cant explain. Bot programs have a basic for loop that does the following:

C#
int i;
for (int n=1;n<999999999;n++)
     i = (n * n) / n; //Small calculation to use some processing power


If I run the C++ or C# program, they average between 14 - 16 seconds, with the C# program usually around .5 seconds quicker. However, when I tried to test the programs by running them at the same time, they finished 50% quicker. This was not a random occurrence. I did this a few times afterwards and it is consistently much faster.

I have never studied at a University level and have only learned from reading books and online material so forgive me if this is a really stupid little thing, but how can this be happening?
Posted
Comments
Philippe Mori 2-Aug-11 20:27pm    
It does not make sense to time a "trivial" program like that to compare the performance of C# vs C++. Whatever the result you obtain from such test would not translate in real life application.

If you do the same code in managed C++ and C#, then the performance should be the same for any pratical purpose.
StM0n 3-Aug-11 2:31am    
Sorry to ask... did you try "pure" C++ or managed... didn't quite get it...
YvesDaoust 3-Aug-11 3:32am    
How exactly do you "run them at the same time" ?
DominicZA 3-Aug-11 4:04am    
Put both the exe's on my desktop, select them both and press enter :P

If both programs need n seconds to run, it doesn't mean you will need 2*n seconds to run both of them at the same time.

If your processor supports hyper-threading, or is a dual-core (or even quad-core), then both programs will run "at the same time" and will need basically still n seconds to finish.

Is it what you mean?

----------------------------

There must be something on your system that make your program wait sometimes. I suggest that you measure the time properly:

Instead of subtracting the ending and starting time of your program, use Process.TotalProcessorTime in C# and GetProcessTimes in regular C (WIN32). I am sure the results will be quite the same, there is absolutely no reason why the same program would run 2 times faster as you describe.
 
Share this answer
 
v2
Comments
DominicZA 2-Aug-11 9:34am    
No, if I run them individually, they take anywhere between 14-16 seconds each. If I run them at the exact same time! They both take between 6-8 seconds.
Olivier Levrey 2-Aug-11 9:56am    
I updated my answer. Please do these simple tests.
My guess is that the OS notices that it is under a certain amount of CPU load and stops running some of its background tasks, allocating more power to completing the tasks you've given it. It is quite odd though.

By the way your arithmetic will result in an overflow and nonsense answers for i > sqrt(MAX_INT), though if you're not using the result I guess you don't care.
 
Share this answer
 
Comments
Philippe Mori 2-Aug-11 20:17pm    
I don't think your answer to be exact but on many recent CPU the speed can be boosted (Turbo) for performance or reduced for economy depending on the charge on the CPU.
If you compile you C++ application in Release mode (fully optimized code) and not managed, it will be much faster as the compiler will realize that the loop is useless (computed value i is never used) and will removed it.

Although I haven't timed the debug version of unmanaged C++, it appears to be roughly similar to C# code (between 8 and 9 seconds on a few years old laptop).
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900