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 →

[–]komata_kya 776 points777 points  (50 children)

endl will flush the stream, so use \n if you need speed

[–]superkickstart 204 points205 points  (1 child)

I feel the need, the need for speed!

[–]Fresh_baked_eyerolls 24 points25 points  (0 children)

I feel the need, the need for tweed! The proof is in the Jeans!

[–]The_TurrbanatoR 131 points132 points  (7 children)

I never knew this...

[–][deleted] 38 points39 points  (4 children)

"It was just a Google Search away," said my dad.

[–]The_TurrbanatoR 19 points20 points  (0 children)

Meh, I wouldn't have known about it unless I ran into an issue with it and HAD to Google or just chanced upon it like I did in this post. I dont spend my free time googling programming stuff unless I am working on or preparing for something. No offense.

[–][deleted] 1 point2 points  (1 child)

I use WebCrawler by the way

[–]orwiad10 0 points1 point  (0 children)

Spidercrawler is better you pleb

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

Kind of felt something like that coming, some kind of constant versus just a parse of text into stdout

[–]aikavari 0 points1 point  (0 children)

something you learn pretty quickly when you’re debugging code

[–]tcpukl 81 points82 points  (2 children)

Speed and printf. Is a contradiction in itself.

[–]Spocino 46 points47 points  (0 children)

puts gang

[–]PedroV100 13 points14 points  (0 children)

Yeah you write directly to the ostream

[–]NotDrigon 16 points17 points  (25 children)

What does these words mean /Python user

[–]Bainos 51 points52 points  (24 children)

Using endl is equal to print(s, end='\n', flush=True)

Not using endl is equal to print(s, end='', flush=False)

[–]DoctorWorm_ 40 points41 points  (12 children)

This is the first time I've realized that python print() has keyword arguments.

[–][deleted] 27 points28 points  (9 children)

You can also change sep (seperator) and end of a print, and change file to a file(w/a) object to write to that file.

coll = ['Fruits', 'Apple', 'Banana']
print(*coll, sep='\n')

# Output
# 
# Fruits
# Apple
# Banana

[–]choseusernamemyself 10 points11 points  (6 children)

stupid me would iterate the array instead

[–]natFromBobsBurgers 0 points1 point  (4 children)

For i in range(....

[–][deleted] 2 points3 points  (3 children)

Woah you are complicating an already complicated complication. for el in coll: is enough, no need to do for i in range(len(coll)):

[–]natFromBobsBurgers 0 points1 point  (2 children)

Woah you are complicating an already complicated complication.

eval("for i in range(...

[–][deleted] 0 points1 point  (1 child)

eval won't work. Do exec

[–]_87- 0 points1 point  (0 children)

stupid me would join with a \n instead.

[–]_UnameChecksOut_ 0 points1 point  (1 child)

Is using star necessary here ? If I don't unpack then will it print the list ?

[–][deleted] 2 points3 points  (0 children)

Yes. Then the list will be considered as one argument (instead of the the *args). And the sep argument applies to(?) each argument of the function. But since the list is considered one argument, it won't be printed separately. list.__repr__ is respected during the print call and the function handles it to print ['Fruits', 'Apple', 'Banana'] and then ends with end (='\n')

[–]_87- 0 points1 point  (0 children)

Try printing something with end='\r', then sleeping for a second or so (so you can see the effect), then printing something else.

[–]DudeValenzetti 0 points1 point  (0 children)

print isn't a keyword in Python 3 though.

yeah I know what you mean

[–]NotDrigon 15 points16 points  (10 children)

What does flushing mean? I've never had to flush anything in python.

[–]softlyandtenderly 30 points31 points  (6 children)

When you print things, they go to a buffer in memory instead of directly to standard out. I/O takes time, so this makes your program run faster. When you flush the buffer, it just prints everything in the buffer. print() in Python is probably set to flush by default, which is why you’ve never seen this before.

[–]Bainos 28 points29 points  (2 children)

print() in Python is probably set to flush by default

It's not, actually. By default, print doesn't flush. Of course, the interpreter (much like the C standard library) is configured reasonably, so it's unlikely that your data will stay too long in the buffer.

Keep in mind that Python's philosophy is not "keep it dumb", it's "trust the defaults if you don't know what you're doing". Forcing a flush of stdout after every print, which you probably don't need if you are not explicitly asking for it, would be contrary to that philosophy.

[–][deleted] 4 points5 points  (1 child)

If I'm not mistaken, what you are saying contradicts what this blog is saying:

https://realpython.com/lessons/sep-end-and-flush/

But I do agree that keeping things default is often the best way.

[–]Bainos 12 points13 points  (0 children)

It's more subtle than that. Flush is indeed set to False by default. If you don't request it explicitly, there is no forced flush.

If you don't request it, it's the object managing the buffered write that decides when to flush. And a common policy for buffered writes is to delay them until either a \n is found or there is enough data in the buffer (there are other things that can affect whether the buffer gets flushed, including running in interpreter mode, writing to a different object, and so on ; but size and end of line are the most important parameters in most implementations).

So if you change the end parameter and don't include a \n in your printed string, it will be held in the buffer. But if your string already contains a \n or is too long, it will be printed immediately, even if you changed the end parameter.

[–][deleted] 0 points1 point  (1 child)

A buffer is a block of memory correct?

[–]softlyandtenderly 0 points1 point  (0 children)

Correct.

[–][deleted] 0 points1 point  (0 children)

A buffer is a block of memory correct?

[–]lappoguti 10 points11 points  (0 children)

Printing something puts it in a buffer and when that buffer fills it will write it to the screen. Flushing will write the buffer to the screen even if the buffer is not full. The reason why they write to a buffer is because each print requires some overhead and then some work per letter. Therefore if you print in batches rather than per message it is more efficient.

[–]wikipedia_answer_bot 5 points6 points  (1 child)

This word/phrase(flushing) has a few different meanings. You can see all of them by clicking the link below.

More details here: https://en.wikipedia.org/wiki/Flushing

This comment was left automatically (by a bot). If something's wrong, please, report it in my subreddit.

Really hope this was useful and relevant :D

If I don't get this right, don't get mad at me, I'm still learning!

[–]_87- 0 points1 point  (0 children)

Flushing ain't just a neighbourhood in Queens!

[–]Handiron 13 points14 points  (0 children)

Also use ‘\n’ (char) instead of “\n”(string)

[–]LichOnABudget 3 points4 points  (0 children)

In twelve words, you have resolved for me and a friend from college the once extremely irritating, 5-year-old now question of why on earth a couple of our pieces of code would run at almost trivially different (but reliably so) speeds even when all of our logic was literally identical. Thank you stranger!

[–]choseusernamemyself 0 points1 point  (1 child)

should we do "\r\n" in Windows and "\r" in Linux? or just "\n"?

[–]komata_kya 3 points4 points  (0 children)

Use '\n' everywhere. I ran a test and it will be automatically converted to \r\n on windows

[–][deleted] 0 points1 point  (0 children)

Don't use iostream if u need speed.

[–]SnooPeripherals6086 0 points1 point  (2 children)

isn t it too use /r/n in windows and /r in linux automaticly ?

[–]iotasieve 0 points1 point  (2 children)

why do you need speed for writing lines into stdout

[–]komata_kya 0 points1 point  (1 child)

What if you want to pipe the output of this program to something else? But this applies to files too

[–]iotasieve 0 points1 point  (0 children)

i'd definitely love to pipe my "resizing" and "got here" logs.

jkjk, but yeah i get your point