Re: Python Speed

Discussion in 'Python' started by Steven D'Aprano, Feb 28, 2013.

  1. On Wed, 27 Feb 2013 21:11:25 -0500, Terry Reedy wrote:

    > There is a problem with timer overhead for sub-microsecond operations.
    > In interactive use, the code is compiled within a function that gets
    > called. The string 'abc需' should be stored as a constant in the code
    > object. To force repeated string operation, one should either time from
    > command line or do an operation, as with the example above. I notice
    > that the first of 3 times is almost always higher for some reason.


    I am not an expert on this, but I suspect the problem may have something
    to do with CPU pipelines and cache. The first time the timer runs, the
    cache is empty, and you get a slightly higher time. Subsequently there
    are not as many CPU cache misses, and the code runs more quickly.

    Or, I could be talking out of my arse. Once upon a time CPUs were simple
    enough for me to understand what make code faster or slower, but no
    more...


    --
    Steven
     
    Steven D'Aprano, Feb 28, 2013
    #1
    1. Advertising

  2. Steven D'Aprano, 28.02.2013 08:05:
    > On Wed, 27 Feb 2013 21:11:25 -0500, Terry Reedy wrote:
    >
    >> There is a problem with timer overhead for sub-microsecond operations.
    >> In interactive use, the code is compiled within a function that gets
    >> called. The string 'abc需' should be stored as a constant in the code
    >> object. To force repeated string operation, one should either time from
    >> command line or do an operation, as with the example above. I notice
    >> that the first of 3 times is almost always higher for some reason.

    >
    > I am not an expert on this, but I suspect the problem may have something
    > to do with CPU pipelines and cache. The first time the timer runs, the
    > cache is empty, and you get a slightly higher time. Subsequently there
    > are not as many CPU cache misses, and the code runs more quickly.


    Well, the default loop iteration count is 1000000, so warming up any caches
    might make a little difference at the very beginning but should rarely have
    a major impact on the overall running time, as each iteration only changes
    the final result by 1/1000000 of its runtime.

    However, it's best to run timeit as a main program (python -m timeit),
    because the way it works then is that it first runs the code a couple of
    times to see how often it should repeat it in a loop to get meaningful
    results. Only *then* it starts benchmarking it. That initial testing phase
    should usually be enough to warm up any caches, so that you'd get better
    results. You can still get the results of all repeated runs if you pass -v.

    Stefan
     
    Stefan Behnel, Feb 28, 2013
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ham

    I need speed Mr .Net....speed

    Ham, Oct 28, 2004, in forum: ASP .Net
    Replies:
    6
    Views:
    2,376
    Antony Baula
    Oct 29, 2004
  2. efiedler
    Replies:
    1
    Views:
    2,136
    Tim Ward
    Oct 9, 2003
  3. Replies:
    2
    Views:
    2,336
    Howard
    Apr 28, 2004
  4. Replies:
    2
    Views:
    353
    Christopher Benson-Manica
    Apr 28, 2004
  5. Weng Lei-QCH1840
    Replies:
    1
    Views:
    201
    Thomas
    Aug 15, 2003
Loading...

Share This Page