0

If you want to know how good your code is...

Example comparing two ways to do the same calculation. Different methods have a 10X impact on the time needed to do the work... Challenge : adapt this to as many languages as possible and publish comparison tables of how fast different languages perform the same computation ! https://code.sololearn.com/cn4f5No80UvM/?ref=app

11th Aug 2017, 6:22 PM
VcC
VcC - avatar
23 odpowiedzi
+ 2
Here is the code for C++: https://code.sololearn.com/cjU3Rn6B37ZE/?ref=app Even if there is only one call from the main() (in C++, the code begin by a function called 'main'), it is the same thing as in Python. However, I had to increase the number of time exp() is run (2,000,000) and did not use random because if the 'exp' functions run with : first 2^3 then a second time with 354.35^49, the execution time won't be the same at all. So I posted the same code as you with this little changes (don't worry, I wrote that you are the author ;) ) It's a good idea and I like it ! Be carreful, execution time isn't constant: if the server is overloaded, execution time will be important.
12th Aug 2017, 4:30 PM
Jojo
+ 2
Yeah, I like the last one guess! It's interesting. They really had to think about security and VM is nice and easiest decision, I think.
12th Aug 2017, 8:16 PM
Andrew Harchenko (Tomsk)
Andrew Harchenko (Tomsk) - avatar
+ 1
I used random call because the time is proportionnal to log(p)+number of '1's in p. Computation times for p= 1023 or 1024 will have a ratio 1 to 2
12th Aug 2017, 4:42 PM
VcC
VcC - avatar
+ 1
yes you're absolutely right, I can see you've add many other way to do x^p I'll try to add them too
12th Aug 2017, 4:42 PM
Jojo
+ 1
It looks like C++ much slower than python ON SOLOLEARN : with x=50.12 and p=999, execution time of basic exp is around 2seconds !! (only 198microseconds in Python !!)
12th Aug 2017, 5:13 PM
Jojo
+ 1
@Andrew When the code is compiled (if it is...), it's not executed: This is (approximately) how compilation works in C++ : code C++ ==> check syntax error, logical error... ==> transform the C++ code to assembly ==> transform assembly to binary .exe(Windows), .elf(Linux)... The code can be executed only when it is binary si the execution time isn't modified by compilation time 😃. Moreover, in my code, it outputs the execution time of a function not of all the code (all is under my control ;) ) But you're right I should have said "Python is faster than C++ for THESE codes and ON SOLOLEARN." 😀
12th Aug 2017, 7:50 PM
Jojo
+ 1
Maybe C++ is slower ON SOLOLEARN (😁) because it is interpreted
12th Aug 2017, 7:53 PM
Jojo
0
Yes this is good for comparison (esp using thousands of calls) probably less for absolute values. Note that it depends onnhow sololearn allocates virtual machines - high load might translate into higher waiting time or lower time limits, not necessarily longer execution time.
12th Aug 2017, 4:40 PM
VcC
VcC - avatar
0
ANY RUBY ADDICT ?
12th Aug 2017, 4:48 PM
VcC
VcC - avatar
0
We should try with a code more complex, with objects
12th Aug 2017, 5:15 PM
Jojo
0
Maybe soloemlearn's C is interpreted rather than compiled...
12th Aug 2017, 6:36 PM
VcC
VcC - avatar
0
@Jojo Oh, sorry! I forgot that your profiling is in code and it's not a time between pressing "run" and finishing. How stupid of me (and lazy to at least open the code or just think a little bit more). :-))
12th Aug 2017, 7:54 PM
Andrew Harchenko (Tomsk)
Andrew Harchenko (Tomsk) - avatar
0
Dont think C can be slower than Python on this kind of stuff, unless it is interpreted or stg is wrong with the time()
12th Aug 2017, 8:03 PM
VcC
VcC - avatar
0
...or just a policy of resource (cpu, memory) sharing on sololearn's servers? Could it be that Python interpreter can use multithreading and does use some threads while compiled C++ code is singlethreaded of course? But with simple tasks it won't give much difference, I think.
12th Aug 2017, 8:05 PM
Andrew Harchenko (Tomsk)
Andrew Harchenko (Tomsk) - avatar
0
Hmmm. In this case if you run the timing with a very small number of function calls you should see a drop in the time per function.
12th Aug 2017, 8:09 PM
VcC
VcC - avatar
0
Or maybe they are running the C code on a virtual machine (ie interpreted pseudo assembly code)...
12th Aug 2017, 8:13 PM
VcC
VcC - avatar
0
I think, as Python is closer to thé computer than C++, it is faster. C++ becomes interesting when the code is much more complex.
12th Aug 2017, 9:38 PM
Jojo
0
How is Python closer to the computer?? When C++ usually has direct access to memory and compiled to machine codes.
12th Aug 2017, 9:39 PM
Andrew Harchenko (Tomsk)
Andrew Harchenko (Tomsk) - avatar
0
I mean Python is closer because (usually) it isn't compiled whereas C++ is read by thé compiler, optimized, turned to assembly sometimes optimized again... When you write a C++ code, you know that if a decompiler existed, it would probably output a different source code than the one you've written... But I totally agree with you to say that C++ is closer to (I would say) the OS.
12th Aug 2017, 10:06 PM
Jojo