+ 3

C++ more than 15 times slower than C#??

I made this little project to check which one of them is faster... in the project: to an array the size of 100.000 the program had to put a factorial of a random number between 9 and 12. the C++ program measured time only for this. the C# program did the same AND!!! every time a new variable was calculated in the array, it saved that array spot in a text folder. (because i had to make sure it didnt skip any arrays because thats how fast it finished its job...) the result? C++ program: 33 s!!! C# program 2s ????? everywhere i read people say that c++ is faster... how is it faster when i get this big difference in results? The odes of the programs i have in my codes as C++ Speed Check and C# speed check. Please look at them and tell me what could be the reason for this. is C++ really that much slower?? https://code.sololearn.com/cs7kFX3cLI6U/#cs https://code.sololearn.com/cpQTcv64VRFA/#cpp

29th Jul 2017, 5:43 AM
Damian Atłasik
Damian Atłasik - avatar
20 ответов
+ 10
@aklex: can i have your brain?
29th Jul 2017, 7:04 AM
jay
jay - avatar
+ 8
The performance hit here is not because of C++ itself. It's because of how slow outputting streams to the console is. Each time you call cout, there is a large overhead, because of all the function calls. cout is very poorly implemented for performance. Just by changing all your couts to printf's, the C++ code will hit 1 second on my end. To add onto that further, you need to realize that everytime you print to the console, you have to make system calls, which means the cpu has to switch to kernel mode and the OS does the printing for you from the kernel. By profiling your code, you will see that most of the cpu time is in fact from the kernel processing your output requests. This is what slows down the code and is the 2nd bottleneck. C++ always has faster raw performance, because it's directly compiled to machine code and the compiler has all the time in the world to generate efficient code. But it doesn't just stop there. The programmer must also know what they are doing in order to be efficient with c++
29th Jul 2017, 7:02 AM
aklex
aklex - avatar
31st Jul 2017, 12:12 PM
jay
jay - avatar
+ 6
Haven't got to a pc yet, but on here (codeplayground) c++ looks faster. As I get to 269 before timeout, where as the c# code only gets to 262 before timeout. Further improvements can be made by removing the calls that are not in your c# code. (gets it to 271 before timeout) https://code.sololearn.com/cVr7S52vVLkA/#cpp using prefix (++i) in factorial squeezes one more to get 272
29th Jul 2017, 6:32 AM
jay
jay - avatar
+ 6
Updated code to remove cout and printfs wow. under 1 second each run..
29th Jul 2017, 7:37 AM
jay
jay - avatar
29th Jul 2017, 2:09 PM
jay
jay - avatar
+ 4
can you show the programs? edit: thanks
29th Jul 2017, 5:40 AM
jay
jay - avatar
+ 4
i will look when I get to my pc. But at first glance. it doesnt look like we are comparing apples with apples. But like I said. I will look when I get to a pc
29th Jul 2017, 5:47 AM
jay
jay - avatar
+ 3
Since I write time sensitive codes for optimization problems, I have aways noticed that console output is very slow. However, I have never profiled the code to check the time was used in kernel mode. Thank you for teaching me that, Aklex. =)
29th Jul 2017, 4:02 PM
Denis Felipe
Denis Felipe - avatar
+ 2
i check the speed on my computer not on this page. also the point is that it need random nr's. you cant go for performance taking out its functionality.
31st Jul 2017, 11:43 AM
Damian Atłasik
Damian Atłasik - avatar
+ 2
The point is how many random numbers you need. If your system is optimizing RNG better for c#, but it is twice as slow as c++ in everything else, you need to check if it is worth using it. Considering it is a game, the time lost at RNG will probably be minimal, so C++ will most likely perform better. But since it is a game, I doubt it will make a big difference to choose c# or c++ in the overall.
31st Jul 2017, 12:08 PM
Denis Felipe
Denis Felipe - avatar
+ 2
ok. just wanted to find out if the current code can get more optimized without the loss in its main functionality. thank you all for the answers.
31st Jul 2017, 12:10 PM
Damian Atłasik
Damian Atłasik - avatar
+ 1
i put in the links to the codes. the avg time n C# comes to 2s/per round and C++ comes to 32s/per round... they both run in a loop 10 times measuring the time each time and saving the time. C++ only sves the time in a text file while C# saves every single array spot of the 100.000 spots each time.
29th Jul 2017, 5:45 AM
Damian Atłasik
Damian Atłasik - avatar
+ 1
i went further: C++ ran 200 rounds of assigning values to arrays of 200.000 and i counted the total time from start to finish.. the total time the program ran 6s. (no printing on screen) the C# version of running the same without printing n screen and not saving each array in text ran 3.2s. also i noticed that if i make an array of 500.000 the c# program takes 8s while the C++ program crashes every single time... what would be your oppinions on that? i updated the codes here so you can look at them.
29th Jul 2017, 1:54 PM
Damian Atłasik
Damian Atłasik - avatar
+ 1
got quiet here. no more ideas why c++ is still slower? not that i want to make it look bad, i prefer c++ over c# but wanted to find out why it is slower in this example ;)
31st Jul 2017, 3:20 AM
Damian Atłasik
Damian Atłasik - avatar
+ 1
Did you just ignore what everyone said? C++ is faster, but the output to console (std::cout and std::cerr) is badly implemented and slow. If you print the results to a file or use printf instead of std::cout, c# will never be faster than c++.
31st Jul 2017, 3:31 AM
Denis Felipe
Denis Felipe - avatar
+ 1
did not ignore anyone. i wrote earlier that withput printing, ONLY calculating, c++ still comes close to 3 times slower than c#. read my comment starting from "i went further". unless you would like me to edit the original question completely.
31st Jul 2017, 3:43 AM
Damian Atłasik
Damian Atłasik - avatar
+ 1
I have just made some experiments here. If you remove the randomization, C++ is twice as fast as C# in the same code. Probably the C# compiler of this page has a better optimization for RNG than c++ compiler of this page. https://code.sololearn.com/cqZFvJrYVIMK/#cpp
31st Jul 2017, 9:24 AM
Denis Felipe
Denis Felipe - avatar
0
I think #c
10th Aug 2017, 12:07 PM
Shivan Sru
Shivan Sru - avatar