Richard Jones wrote:
On Sun, 17 Jun 2007, Richard Jones wrote:
I presume you were just calling float(value)? I'm surprised that is slower
than invoking isinstance() if value is a float.
And a quick check tells me my gut was about right: there is no time difference
between these if value is a float:
$ python -m timeit 'isinstance(1.0, float)'
1000000 loops, best of 3: 0.34 usec per loop
$ python -m timeit 'float(1.0)'
1000000 loops, best of 3: 0.344 usec per loop
Of course your code uses an assert for the isinstance which will be removed
when Python's invoked with "-O"...
I get slightly different results.
C:\Documents and Settings\Administrator>python -m timeit "float(1.0)"
1000000 loops, best of 3: 0.711 usec per loop
C:\Documents and Settings\Administrator>python -m timeit "float(1)"
1000000 loops, best of 3: 0.782 usec per loop
C:\Documents and Settings\Administrator>python -m timeit
"isinstance(1.0, float)
"
1000000 loops, best of 3: 0.598 usec per loop
Although, I think its possibly too close to call, and all I can really
conclude is that you have a faster computer than mine!
I think Greg's idea of multiplying by 1.0 may be best from the interface
point of view.
Will