Charles Noon
Charles Noon

Reputation: 601

How long does printing to the screen take?

I have a program that goes through everything in a huge list (~14 meg), and performs an operation with each item. I noticed that this took longer when I had it print each item as it iterated over it. So that got me to wondering - how long does it take to print something to the screen? Or more specifically, how much will it slow me down? Is it 'worth it'?

Upvotes: 3

Views: 6907

Answers (2)

ALH
ALH

Reputation: 1

Look into using the timeit module.

import timeit

Check the difference between these two calls. In these examples number is the number of times the code in stmt is called.

timeit.timeit(stmt='print(str1[:5])',setup='str1 = "Hello, World!"',number=1000)

vs.

timeit.timeit(stmt='str1[:5]',setup='str1 = "Hello, World!"',number=1000)

Upvotes: 0

javidcf
javidcf

Reputation: 59731

You can easily measure this kind of things with IPython %timeit magic function (see a tutorial here):

In [6]: %timeit print('', end='')
100000 loops, best of 3: 3.44 µs per loop

Obviously, the actual result will depend on many factors, but you can do some naive benchmarking with this.

Upvotes: 1

Related Questions