Posts: 4,647
Threads: 1,494
Joined: Sep 2016
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.
Posts: 325
Threads: 11
Joined: Feb 2010
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
Posts: 101
Threads: 5
Joined: Jul 2019
(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
Posts: 325
Threads: 11
Joined: Feb 2010
My code was using the timeit() function from the timeit module:
from timeit import timeit
Posts: 7,320
Threads: 123
Joined: Sep 2016
Aug-26-2019, 08:46 AM
(This post was last modified: Aug-26-2019, 08:46 AM by snippsat.)
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.
Posts: 101
Threads: 5
Joined: Jul 2019
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!
Posts: 7,320
Threads: 123
Joined: Sep 2016
Aug-26-2019, 10:49 AM
(This post was last modified: Aug-26-2019, 10:49 AM by snippsat.)
(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.
Posts: 4,647
Threads: 1,494
Joined: Sep 2016
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.
Posts: 7,320
Threads: 123
Joined: Sep 2016
Aug-26-2019, 07:43 PM
(This post was last modified: Aug-26-2019, 07:44 PM by snippsat.)
(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.
Posts: 4,647
Threads: 1,494
Joined: Sep 2016
(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.
|