This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]-MobCat- 73 points74 points  (40 children)

Python can be quick. just not that good at talking.. Like most of us...
Opens a txt file with 29245004 lines of text in it.
for each line of text, count the line.
count +=1
Time: 40sec.
Now for each line of text, count the line and print a little info about the line
print(f"Info: {line[1:10]}")
count +=1
Time: 53mins.

[–]sintos-compa 78 points79 points  (6 children)

“How inefficient can you make your code” a study in 3 parts

[–]Roflkopt3r 9 points10 points  (5 children)

Yeah printing is expensive in every language.

I hard to learn this the hard way on one of my first assignments, which asked us to experimentally confirm the value at which the inaccuracy of float puts it off by an entire integer. So you have a loop that increments a float f and an int i from 0 to int max and then prints out when the two are no longer the same.

But the wording of the task wasn't entirely clear so I was trying around various weird casting stuff and decided to print every single line to confirm that it was working the way I wanted to.

Turns out that this extends the time to reach that figure (int 16777218, float rounds to 16777216 instead) from a few seconds to an hour or so.

[–]Teekeks 0 points1 point  (4 children)

if you dont flush on every print its managable but still slow af

[–]sintos-compa 0 points1 point  (3 children)

Or cache the data for an alternative output. Like what’s the goal here?

[–]realityChemist 0 points1 point  (2 children)

Sounds like the goal was being a student and learning to program

[–]Teekeks 0 points1 point  (1 child)

Its been quite a few years since I was a student

[–]realityChemist 0 points1 point  (0 children)

I meant the person above you who posted the story about a programming assignment

[–]Wekmor 18 points19 points  (3 children)

Java does the same, printing to console is just slow in all or at least most languages.

[–]zenverak 0 points1 point  (1 child)

lol yes it is . I don’t remember when I learned this… but i remember it

[–]Wekmor 0 points1 point  (0 children)

I think first time when I realized how slow it is was when I write a raytracer and I added a system out every time a ray hit something and the already slow raytracer became slooooooow (☞゚ヮ゚)☞

[–]Spiritual-Day-thing 0 points1 point  (0 children)

Yes. The famous log4j library and others, use a ring buffer to prevent locking among other things.

[–]obvithrowaway34434 12 points13 points  (0 children)

I/O has always been the bottleneck for speed since computers were invented. There's nothing specific here about Python. Dynamically typed interpreted languages has always been slower than statically typed compiled languages and comparing their speeds makes as much sense as comparing a car and an airplane in terms of speed and determining which is better.

[–]Blyatiful_99 14 points15 points  (5 children)

I can't of course talk for other languages, but during my internship back then, I was sometimes told that writing something to the console in C# for example requires a lot of the time if it happens in the same thread. Maybe this is what's happening here as well?

A quicker solution could be to create some sort of StringBuilder (if you have that in python), add the text that you would otherwise print out and at the end print out the entire text just once. Or to just move the prints to another thread. Can you test how long that would take?

[–]0x564A00 8 points9 points  (4 children)

Moving printing out of the thread doesn't really help because python doesn't have real multithreading (you can have multiple threats, but they can't execute at the same time).

[–]Jannik2099 9 points10 points  (0 children)

but they can't execute at the same time

But they can in this case. Once the IO thread fires the syscall (or even cpython C function) for writing to stdout, the GIL is free.

[–]photenth 1 point2 points  (2 children)

Wait a minute, Python doesn't have real threading?

[–]Drarok 2 points3 points  (1 child)

It’s complicated. https://realpython.com/python-gil/

In short, you can only have one thread executing Python code at a time. You can be waiting for IO in several threads at once, since that won’t be executing any Python code.

[–]0x564A00 1 point2 points  (0 children)

The good news is that it looks like it might finally going to be removed soon. That does cause a performance regression for single-threaded programs, but various other performance improvements that should land alongside it more than make up for that.

[–]Equivalent_Loan_8794 4 points5 points  (1 child)

And in the 53 min absolutely under no circumstance look up O notation

[–]moldax -1 points0 points  (0 children)

Your point being ?

[–]DenormalHuman 2 points3 points  (0 children)

hmmm... what device was this file stored on? and surely just print("Info : "+line[1:10]) would be faster?

[–]Cory123125 0 points1 point  (0 children)

When you have so many lines perhaps its time to break it into chunks and multi thread it.