C++ vs Python Speed Test

preview_player
Показать описание
Рекомендации по теме
Комментарии
Автор

One thing for sure, the author of this video is a very bad programmer and has no idea of how I/O works.

youuuuuuuuuuutube
Автор

1. endl flushes the buffer, making it slower
2. C++ is printing everything line by line while python is printing chunks by chunks. It's doing some optimization. If you apply the same optimization with c++ it'll be faster than python. Use char buffer[] and fwrite.
Also try printing it to a document instead, here's the code for c++
#include<iostream>
int main() {
freopen("data.out", "w", stdout);
for (int i{}; i != 1e4; i++) {
std::cout << i << '\n';
}
}

xiangli
Автор

C++ is doing line by line, while Python is doing chunks.
C++ is about 400 times faster than Python . O_O

shang_psycho
Автор

A noobie mistake. C++ is a powerful language on the hands of a skilled programmer. You're really testing python's ability to handle poor programing design on I/O.

Try testing two CPU intensive algorithms such as prime numbers or even some graph solving algorithms. You'll see the difference can get to 10x faster in favor of C++

cristianoo
Автор

pfft.... You don't have to output things to the screen. Just do this:

C++ :

#include <iostream>


//main function (entry point)
int main(int argc, char** argv)
{
register short int i = 0; // registers the block of memory to the cpu cage, thus address is not accessible.

while(i < 100, 000, 000) i++; // NO NEED TO PRINT
std::cout << "done";

return 0;
}


Python:

i=0
while i < 100, 000, 000:
i++;
print("done")



This way the computer's speed isn't affected by printing things to the screen. What you are doing proves that you are not a programmer.

purple.requiem
Автор

Create a char* buffer, maybe size 4096. Then print that to stdout all at once.
C++ is much faster than Python.
You can’t control the buffer sizes Python creates so C++ wins.

Bangy
Автор

This is irrelevant, C++ is far superior in speed than Python. For real, I cannot imagine anyone comparing them. C++ is compiled, probably faster, and std::endl just makes it even slower and the other hand python is writing chunks and not line by line. Moreover, using a namespace only slows down the code and causes bugs

frosty
Автор

i did this comparison and made them both print out to 1 million, results:
python: 1 minute and 58 seconds
c++: 1 minute and 25 seconds

jamlie
Автор

I am solving some problems, and one of them gave 80% in Python (20% time limit exceed) and 100% in C++

sadiqmlikov
Автор

Don't display anything for benchmarks. There is abstraction, buffer, background processes on the kernel stack. Do many calculations, run many times, look at avg/min/max. The for loops are horrible also. Windows is poor at cpu load balancing as well. A compiled binary should be generally faster than an interpreted language. If you do a calculation with two variables, you should see that Python's dynamic typing is slower as program depth increases, nevermind thread management. I still prefer python mixed with C. C++ syntax and code optimizing is just tedious to me. Choice is a time mgmt question. Python is fun and I can't believe big companies used it in production. Apples and Oranges. [Edit] I forgot to mention, simply, a C++ vs Cython, is more interesting since you can go in and statically type in Cython, carving off time exponentially at times. I would like to find a board and be able to test Arduino vs MicroPython. Idk if thats possible or relevant for hardware projects.

greycell
Автор

everyone, the problem here isnt the for loop, the debug console is just faster than terminal or cmd
you can check that on your ide in any language > try terminal run file.name then try the run shortcut on the ide ;

addictedtocode
Автор

It's not fair at all, you should not be using endl because it flushes every time. Also you should do io::sync_with_stdio(false);

Or just use std::printf()

destiny_
Автор

Lol. Try to task without console ouptut. (for example, find first 40000 prime numbers ).

neutralbbname
Автор

Is this meant to be a joke or something? I should drop a dislike here, but I'm not doing that, under the assumption that this video was made by a beginner who's literally just hopped off a session of programming 101 video tutorials and is excited about experimenting. I'm not leaving a like either, as this is highly misleading to someone who can't tell how different those two applications and even the terminals are.

Just so you know: done the right way and with real calculations added, tests usually show C/C++ runs 500, even 800 times faster than Python. There's no actual usefulness in trying to prove that wrong, as Python exists to fulfill other needs, it never came to be with the purpose of being faster than C/C++ in the first place.

lean.drocalil
Автор

Slow down you guys.
Test was a joke.

Youtuber cleared her/himself here 0:35

irfanjames
Автор

Differences in console work ide and windows.
Разница между работой консолей.

On linux Konsole no difference because everything the output rate of the text in the console.
В линукс Konsole нету разницы потому чттсе упирается в скорость вывода текста консолью.

For normal comprasion, you need a job, let's calculate 100.000 SHA256 and then c++ pills Python.
Для нормального сравнения нужна работа, скажем дать вычислять 100.000 SHA256 и тогда с++ порвёт Пайтон.

daniil
Автор

At least print in a file to test how these languages handle working with files.

What you have actually shown is how fast the terminals are, not how fast the languages can print these numbers and especially not how fast they run.

Hardcore_Remixer
Автор

This dude really defined "i" before the for loop 💀

playstationrussia
Автор

Most useless way to compare for speed. It involves IO operation which depends on the system and not on the language. It can be fast or slow depending on the current state of your system.

DKeshavPri
Автор

In my laptop it was less than a 20 milisecond for printing 1to10000 using loop, c/cpp both language

six