Skip to content

Quantity(35.0, 'uA') takes 90!! times longer than np.array(35.0) #63

Open

Description

@twmr

The problem I see with the quantities package is that the overhead of creating scalar quantities is too high compared to instantiating a scalar numpy array. In my case (numpy version 1.7.1 - 64bit fedora 19) Quantity(35.0, 'uA') takes ~90!!! times longer than np.numpy(35.0).

from timeit import Timer
import numpy as np

number = int(1e4)
a = Timer('np.array(35.0)',
          setup='import numpy as np').repeat(number=number)
print "np took", 1e6*(np.array(a).min()), "usec"
b = Timer('pq.Quantity(35.0, "uA")',
          setup='import quantities as pq').repeat(number=number)
print "pq took", 1e6*(np.array(b).min()), "usec"

print "ratio", np.array(b).min()/np.array(a).min()

outputs:
np took 3584.14649963 usec
pq took 307501.077652 usec
ratio 85.7947848068

The major hot spot is this line https://github.com/python-quantities/python-quantities/blob/master/quantities/quantity.py#L127. If this gets commented out the ratio reduces from 85 to 15.

The next (minor) hot spot is https://github.com/python-quantities/python-quantities/blob/master/quantities/quantity.py#L126: If I compare the timings of np.array(35.5) with np.array(35.0).view(pq.Quantity) I get a ratio of 11.

c = Timer('np.array(35.0).view(pq.Quantity)',
          setup='import numpy as np; import quantities as pq').repeat(number=number)
print "np view took", 1e6*(np.array(c).min()), "usec"
print "ratio (view)", np.array(c).min()/np.array(a).min()

This high ratio is due to
https://github.com/python-quantities/python-quantities/blob/master/quantities/quantity.py#L214. It reduces to 3.7 if this method gets commented out.

As I heavily rely upon the quantities package in one of my python packages and I do not have any experience with subclassing ndarrays I would like to see this fixed. If you give me some comments I could come up with a PR.

Activity

  1. derekbrokeit commented on Jul 16, 2013

    @derekbrokeit

    I noticed similar (although my machine is ~40-60 times, not 90). I did a test, and it seems you can get nearly twice the speed using quantities if you do not use string values for units.

    Try adding this to get a little more speed without modifying quantities:

    c = Timer('pq.Quantity(35.0, pq.uA)',
              setup='import quantities as pq').repeat(number=number)
    print "unit took", 1e6*(np.array(c).min()), "usec"
    print "b/c", np.array(b).min()/np.array(c).min()
    

    My output:

    b/a 61.2311716921
    c/a 37.1575565652
    b/c 1.64787939123
    
  2. derekbrokeit commented on Jul 16, 2013

    @derekbrokeit

    Ah, even better:

    d = Timer('pq.Quantity(35.0, pq.uA.dimensionality)',
              setup='import quantities as pq').repeat(number=number)
    print "dimensionality took", 1e6*(np.array(d).min()), "usec"
    

    results:

    p took 5995.03517151 usec
    pq took 320407.152176 usec
    unit size took 209959.030151 usec
    dimensionality size took 116689.920425 usec
    b/a 53.4454165838
    b/c 1.52604606692
    b/d 2.74579973153
    c/a 35.0221515212
    d/a 19.4644263273
    
  3. twmr commented on Jul 16, 2013

    @twmr
    ContributorAuthor

    I have noticed the same thing (not the one with the dimensionality). Even with your suggestions the code is still 20 times slower than numpy. I will have a look at how the creation of quantities objects is benchmarked in tbekolay/quantities-comparison (see http://pyvideo.org/video/2012/a-comprehensive-look-at-representing-physical-qua) because the make column of the beanchmark in http://www.youtube.com/watch?feature=player_detailpage&v=N-edLdxiM40#t=804s shows a ratio of 1.0 between quantities and numpy...

  4. derekbrokeit commented on Jul 18, 2013

    @derekbrokeit

    I just want to add that the benchmarks in that presentation are probably wrong and the presenter says "take it with a grain of salt". For one, many of those packages he tested wrap numpy, which means that they create a numpy array AND do something else.For quantities, it must create itself (an ndarray) AND setup it's dimensionality. I think that figure in the case of constructors must be mistaken.

  5. derekbrokeit commented on Jul 18, 2013

    @derekbrokeit

    So, I looked into this breifly, numpy arrays obviously don't take that long to make. That being said if you create lots of little Quantities they can become time consuming.

    You mention that the primary bottleneck appears to be the dimensionality validation function. Therefore, I suggested that you add a dimensionality, but this can be further sped up (x2) by using pq.uA._dimensionality (with underscore) because there is only one object copy. Using pq.uA.dimensionality (without underscore) performs 2 copies of _dimensionality. This would be good in code that is embedded in a module and not user-facing.

    Another point, is that you are testing a single element numpy array, which takes 0.0059 seconds to create 10,000 arrays (your example) ... That isn't a lot of time. When you start to create very large arrays, the ratio significantly decreases until it is less that 2x for a Quantity. If you are creating a Quantity from some numpy function, it is best to specify copy=False in the constructor.

    Consider this example:

    from timeit import Timer
    import numpy as np
    
    number = int(1e4)
    a = Timer('np.copy(x)',
                      setup='import numpy as np; x = np.arange(100000)').repeat(number=number)
    print "np took", 1e6*(np.array(a).min()), "usec"
    b = Timer('pq.Quantity(x, "m")',
                      setup='import quantities as pq;import numpy as np; x=np.arange(100000)').repeat(number=number)
    print "pq took", 1e6*(np.array(b).min()), "usec"
    e = Timer('pq.Quantity(x, pq.m._dimensionality)',
                      setup='import quantities as pq;import numpy as np; x=np.arange(100000)').repeat(number=number)
    print "_dim took", 1e6*(np.array(e).min()), "usec"
    f = Timer('x=np.arange(100000) ; pq.Quantity(x, pq.m._dimensionality, copy=False)',
                      setup='import quantities as pq;import numpy as np').repeat(number=number)
    print "_dim copy", 1e6*(np.array(f).min()), "usec"
    
    print "b/a", np.array(b).min()/np.array(a).min()
    print "e/a", np.array(e).min()/np.array(a).min()
    print "f/a", np.array(f).min()/np.array(a).min()
    

    Output:

    np took 592884.063721 usec
    pq took 984493.017197 usec
    _dim took 735006.809235 usec
    _dim copy 581083.059311 usec
    b/a 1.6605152296
    e/a 1.23971422781
    f/a 0.98009559519
    

    As you can see performing a standard numpy.copy operation on an ndarray takes more time (probably about the same) than creating a Quantity by using the same memory as the numpy created array.

    This is the method you should be using in your code, I think. For what it's worth, this is probably the method being used by the presenter of that video you posted.

  6. apdavison commented on Nov 17, 2026

    @apdavison
    Contributor

    This is still a problem: with the current master and NumPy 2.5, pq.Quantity(35.0, "uA") takes about 75 times as long as np.array(35.0), and pq.Quantity(35.0, pq.uA) about 15 times.

    #260 gives a more recent use case that ran into this problem.

    I plan to work on this in the near future.

  7. added
    performance
    on Nov 17, 2026
  8. added this to the 0.18.0 milestone on Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    performance

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions