Python Forum
print a list strings is fast but printing the joined strings is slow
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
print a list strings is fast but printing the joined strings is slow
#1
i did the following:
Output:
>>> a=['01'[len(repr(chr(x)))==3] for x in range(1114112)] >>> b=''.join(a)
then i did print(a) and it printed quite fast, but then i did print(b) expecting it to be slightly faster or in the worst case just as fast. but, in reality, the 2nd print was several times slower. did Python3 cause this slowness?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
No idea why you get the result you do, but for me, printing b is about 5 times faster:
>>> timeit('print(a)', 'from __main__ import a', number=1)
(...)
4.1929473
>>> timeit('print(b)', 'from __main__ import b', number=1)
(...)
0.7649681999999984
Reply
#3
(Aug-26-2019, 05:40 AM)stranac Wrote: No idea why you get the result you do, but for me, printing b is about 5 times faster:
 >>> timeit('print(a)', 'from __main__ import a', number=1) (...) 4.1929473 >>> timeit('print(b)', 'from __main__ import b', number=1) (...) 0.7649681999999984 

I'm in learning stage about Python. timeit is completely new module for me and I thought of giving it a try but I'm observing error when trying to run it

>>> a='yes'
>>> timeit('print(a)','from __main__ import a',number=1)
Error:
Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: 'module' object is not callable
Reply
#4
My code was using the timeit() function from the timeit module:
from timeit import timeit
Reply
#5
print() should not be used when measure code,i never use it.
Then it get a competition how fast the medium you use can print to screen,
this can vary quite a lot between editor/shell and also OS used.

In general print(b) should be faster,is that for me.
VS Code do both in under 1-sec,terminal and other editor it can go up to 17-sec as highest for me.
As mention this is a contest of editor/shell to print a lot to screen,rarely useful at all in the real world.
Reply
#6
alright! it is working now and I need a clarity

>>> a= 'okay'
for i in range(4): timeit('print(a)','from __main__ import a',number=4)
Output:
>>> for i in range(4): timeit('print(a)','from __main__ import a',number=4) ... okay okay okay okay 0.0035411143718420135 okay okay okay okay 0.0012378038995848328 okay okay okay okay 0.0006817251627353471 okay okay okay okay 0.001336081797035149 >>>
So, here it is displaying timer after printing okay for 4 times. It that a cumulative timer to print all the 4 okies or how it working??. Please explain..! Thanks in advance!
Reply
#7
(Aug-26-2019, 09:03 AM)Malt Wrote: here it is displaying timer after printing okay for 4 times.
You don't need loop with timeit is doing loop in number=4 .
It run the code 4 times and give average time back.
Be using print() as mention you get different result back.
This is not how fast the code run,but the printing to screen in interactive shell in this case.

Here a more realistic example there where a discussion a while back about which was faster,word[0] or startswith().
See that i don't use print() here i want to measure code speed,and not the printing to screen.
import timeit

def time_1(word):
    if word[0] in ('w','x','y','z'):
        f'word <{word}> has a first letter in sequence <wxyz>'

def time_2(word):
    if word[0] in 'wxyz':
        f'word <{word}> has a first letter in sequence <wxyz>'

def time_3(word):
    if word.startswith(('w','x','y','z')):
        f'word <{word}> has a first letter in sequence <wxyz>'

lst = ['time_1', 'time_2', 'time_3']
for test in lst:
    t = timeit.Timer(f"{test}('war'*5)", f'from __main__ import {test}').timeit(number=10000000)
    print(f'{test} --> {t:.2f}')
So this run each code in function 10000000 times,and we get average time back.
Output:
E:\div_code λ python time_startswith.py time_1 --> 3.49 time_2 --> 3.53 time_3 --> 5.01
So word[0] is a little faster than startswith().
Dos this mean that should use word[0]?
My answer was no because startswith() is more readable and the time differences is really small.
Reply
#8
i ran them separate with stdout redirected to files and got same timing (very close). cat those files and got the timing issue. so, it must be some terminal issue.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#9
(Aug-26-2019, 07:22 PM)Skaperen Wrote: it must be some terminal issue.
As i try to explain never use print() when measure code,if do can get result that can vary a lot depend on what use Terminal/Editor or OS.
Reply
#10
(Aug-26-2019, 07:43 PM)snippsat Wrote:
(Aug-26-2019, 07:22 PM)Skaperen Wrote: it must be some terminal issue.
As i try to explain never use print() when measure code,if do can get result that can vary a lot depend on what use Terminal/Editor or OS.
i wasn't trying to measure it until the funny timing happen. i still don't know why that kind of difference affects the terminal program like that. it's strange. but, apparently, not a python issue.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python Mapped Strings edualfaia 12 1,920 Sep-04-2023, 11:08 AM
Last Post: snippsat
Question bytes and strings Skaperen 0 721 Aug-28-2023, 02:58 AM
Last Post: Skaperen
  f-strings round float down too much Skaperen 5 2,523 Dec-29-2021, 08:37 PM
Last Post: Skaperen
  f-strings in a script to be made public Skaperen 2 2,101 Jan-03-2020, 06:03 PM
Last Post: Skaperen
  supporting both strings and bytes in functions Skaperen 0 1,374 Nov-28-2019, 03:17 PM
Last Post: Skaperen
  side effects in f-strings Skaperen 0 1,656 Sep-08-2019, 01:26 AM
Last Post: Skaperen
  trying to learn f-strings - puzzled by format() protocol Skaperen 7 3,577 Jul-26-2019, 08:40 PM
Last Post: Skaperen
  mixing bytes with strings Skaperen 3 2,371 May-29-2019, 02:30 AM
Last Post: heiner55
  to strings or to bytes, that is the question Skaperen 0 2,008 Jul-07-2018, 08:35 PM
Last Post: Skaperen
  mutable strings Skaperen 3 3,460 Dec-03-2017, 03:05 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020