Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #10077 > unrolled thread
| Started by | Stefan Behnel <stefan_ml@behnel.de> |
|---|---|
| First post | 2011-07-22 08:37 +0200 |
| Last post | 2011-07-22 23:30 -0700 |
| Articles | 7 — 4 participants |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: Question about timeit Stefan Behnel <stefan_ml@behnel.de> - 2011-07-22 08:37 +0200
Re: Question about timeit Frank Millman <frank@chagford.com> - 2011-07-21 23:59 -0700
Re: Question about timeit Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> - 2011-07-22 10:08 +0200
Re: Question about timeit Stefan Behnel <stefan_ml@behnel.de> - 2011-07-22 10:34 +0200
Re: Question about timeit Frank Millman <frank@chagford.com> - 2011-07-22 05:30 -0700
Re: Question about timeit Thomas Jollans <t@jollybox.de> - 2011-07-22 14:43 +0200
Re: Question about timeit Frank Millman <frank@chagford.com> - 2011-07-22 23:30 -0700
| From | Stefan Behnel <stefan_ml@behnel.de> |
|---|---|
| Date | 2011-07-22 08:37 +0200 |
| Subject | Re: Question about timeit |
| Message-ID | <mailman.1350.1311316646.1164.python-list@python.org> |
Frank Millman, 22.07.2011 08:06:
> I mentioned in a recent post that I noticed an inconsistency in timeit, and
> then reported that I must have made a mistake.
>
> I have now identified my problem, but I don't understand it.
>
> C:\Python32\Lib>timeit.py "int(float('165.0'))"
> 100000 loops, best of 3: 3.52 usec per loop
>
> C:\Python32\Lib>timeit.py "int(float('165.0'))"
> 100000 loops, best of 3: 3.51 usec per loop
>
> C:\Python32\Lib>timeit.py 'int(float("165.0"))'
> 10000000 loops, best of 3: 0.0888 usec per loop
>
> C:\Python32\Lib>timeit.py 'int(float("165.0"))'
> 10000000 loops, best of 3: 0.0887 usec per loop
>
> I ran them both twice just to be sure.
>
> The first two use double-quote marks to surround the statement, and
> single-quote marks to surround the literal inside the statement.
>
> The second two swap the quote marks around.
>
> Can someone explain the difference?
>
> I am using python 3.2 on Windows Server 2003.
As expected, I can't reproduce this (on Linux). Maybe your processor
switched from power save mode to performance mode right after running the
test a second time? Or maybe you need a better console application that
handles quotes in a more obvious way?
Note that it's common to run timeit like this: "python -m timeit".
Stefan
[toc] | [next] | [standalone]
| From | Frank Millman <frank@chagford.com> |
|---|---|
| Date | 2011-07-21 23:59 -0700 |
| Message-ID | <83238f45-e72d-4ea9-9b4a-a42d26cd7282@x7g2000vbk.googlegroups.com> |
| In reply to | #10077 |
On Jul 22, 8:37 am, Stefan Behnel <stefan...@behnel.de> wrote:
> Frank Millman, 22.07.2011 08:06:
>
>
>
>
>
> > I mentioned in a recent post that I noticed an inconsistency in timeit, and
> > then reported that I must have made a mistake.
>
> > I have now identified my problem, but I don't understand it.
>
> > C:\Python32\Lib>timeit.py "int(float('165.0'))"
> > 100000 loops, best of 3: 3.52 usec per loop
>
> > C:\Python32\Lib>timeit.py "int(float('165.0'))"
> > 100000 loops, best of 3: 3.51 usec per loop
>
> > C:\Python32\Lib>timeit.py 'int(float("165.0"))'
> > 10000000 loops, best of 3: 0.0888 usec per loop
>
> > C:\Python32\Lib>timeit.py 'int(float("165.0"))'
> > 10000000 loops, best of 3: 0.0887 usec per loop
>
> > I ran them both twice just to be sure.
>
> > The first two use double-quote marks to surround the statement, and
> > single-quote marks to surround the literal inside the statement.
>
> > The second two swap the quote marks around.
>
> > Can someone explain the difference?
>
> > I am using python 3.2 on Windows Server 2003.
>
> As expected, I can't reproduce this (on Linux). Maybe your processor
> switched from power save mode to performance mode right after running the
> test a second time? Or maybe you need a better console application that
> handles quotes in a more obvious way?
>
> Note that it's common to run timeit like this: "python -m timeit".
>
> Stefan- Hide quoted text -
>
> - Show quoted text -
I tried "python -m timeit", and got exactly the same result as before.
I am using a desktop, not a laptop, so there is no power-saving mode
going on.
I am using the standard Windows 'Command Prompt' console to run this.
I tried it with python 2.6, and still get the same result.
My guess is that it is something to do with the console, but I don't
know what. If I get time over the weekend I will try to get to the
bottom of it.
Frank
[toc] | [prev] | [next] | [standalone]
| From | Thomas Rachel <nutznetz-0c1b6768-bfa9-48d5-a470-7603bd3aa915@spamschutz.glglgl.de> |
|---|---|
| Date | 2011-07-22 10:08 +0200 |
| Message-ID | <j0bb5t$11c$1@r03.glglgl.eu> |
| In reply to | #10079 |
Am 22.07.2011 08:59 schrieb Frank Millman:
> My guess is that it is something to do with the console, but I don't
> know what. If I get time over the weekend I will try to get to the
> bottom of it.
I would guess that in the first case, python (resp. timeit.py) gets the
intended code for execution: int(float('165.0')). I. e., give the string
to float() and its result to int().
In the second case, however, timeit.py gets the string
'int(float("165.0"))' and evaluates it - which is a matter of
sub-microseconds.
The reason for this is that the Windows "shell" removes the "" in the
first case, but not the '' in the second case.
Thomas
[toc] | [prev] | [next] | [standalone]
| From | Stefan Behnel <stefan_ml@behnel.de> |
|---|---|
| Date | 2011-07-22 10:34 +0200 |
| Message-ID | <mailman.1353.1311323659.1164.python-list@python.org> |
| In reply to | #10083 |
Thomas Rachel, 22.07.2011 10:08:
> Am 22.07.2011 08:59 schrieb Frank Millman:
>
>> My guess is that it is something to do with the console, but I don't
>> know what. If I get time over the weekend I will try to get to the
>> bottom of it.
>
> I would guess that in the first case, python (resp. timeit.py) gets the
> intended code for execution: int(float('165.0')). I. e., give the string to
> float() and its result to int().
>
> In the second case, however, timeit.py gets the string
> 'int(float("165.0"))' and evaluates it - which is a matter of
> sub-microseconds.
>
> The reason for this is that the Windows "shell" removes the "" in the first
> case, but not the '' in the second case.
Good call. Or maybe it actually gets the code 'int(float(165.0))' in the
second case, so it doesn't need to parse the string into a float. But given
the huge difference in the timings, I would second your guess that it just
evaluates the plain string itself instead of the code.
Stefan
[toc] | [prev] | [next] | [standalone]
| From | Frank Millman <frank@chagford.com> |
|---|---|
| Date | 2011-07-22 05:30 -0700 |
| Message-ID | <ed943cd7-1986-4e09-a062-a084ef623c73@fq4g2000vbb.googlegroups.com> |
| In reply to | #10086 |
On Jul 22, 10:34 am, Stefan Behnel <stefan...@behnel.de> wrote:
> Thomas Rachel, 22.07.2011 10:08:
>
>
>
>
>
> > Am 22.07.2011 08:59 schrieb Frank Millman:
>
> >> My guess is that it is something to do with the console, but I don't
> >> know what. If I get time over the weekend I will try to get to the
> >> bottom of it.
>
> > I would guess that in the first case, python (resp. timeit.py) gets the
> > intended code for execution: int(float('165.0')). I. e., give the string to
> > float() and its result to int().
>
> > In the second case, however, timeit.py gets the string
> > 'int(float("165.0"))' and evaluates it - which is a matter of
> > sub-microseconds.
>
> > The reason for this is that the Windows "shell" removes the "" in the first
> > case, but not the '' in the second case.
>
> Good call. Or maybe it actually gets the code 'int(float(165.0))' in the
> second case, so it doesn't need to parse the string into a float. But given
> the huge difference in the timings, I would second your guess that it just
> evaluates the plain string itself instead of the code.
>
> Stefan- Hide quoted text -
>
> - Show quoted text -
This is what I get after modifying timeit.py as follows -
if args is None:
args = sys.argv[1:]
+ print(args)
C:\>python -m timeit int(float('165.0'))
["int(float('165.0'))"]
100000 loops, best of 3: 3.43 usec per loop
C:\>python -m timeit int(float("165.0"))
['int(float(165.0))']
1000000 loops, best of 3: 1.97 usec per loop
C:\>python -m timeit "int(float('165.0'))"
["int(float('165.0'))"]
100000 loops, best of 3: 3.45 usec per loop
It seems that the lesson is -
1. Use double-quotes around the command itself - may not be necessary
if the command does not contain spaces.
2. Use single-quotes for any literals in the command.
Frank
[toc] | [prev] | [next] | [standalone]
| From | Thomas Jollans <t@jollybox.de> |
|---|---|
| Date | 2011-07-22 14:43 +0200 |
| Message-ID | <mailman.1364.1311338605.1164.python-list@python.org> |
| In reply to | #10102 |
On 22/07/11 14:30, Frank Millman wrote:
> This is what I get after modifying timeit.py as follows -
>
> if args is None:
> args = sys.argv[1:]
> + print(args)
>
> C:\>python -m timeit int(float('165.0'))
> ["int(float('165.0'))"]
> 100000 loops, best of 3: 3.43 usec per loop
>
> C:\>python -m timeit int(float("165.0"))
> ['int(float(165.0))']
> 1000000 loops, best of 3: 1.97 usec per loop
>
> C:\>python -m timeit "int(float('165.0'))"
> ["int(float('165.0'))"]
> 100000 loops, best of 3: 3.45 usec per loop
>
> It seems that the lesson is -
>
> 1. Use double-quotes around the command itself - may not be necessary
> if the command does not contain spaces.
> 2. Use single-quotes for any literals in the command.
What about 'int(float("165.0"))' (single quotes around the argument)?
Does that pass the single quotes around the argument to Python? Or does
it eliminate all quotes?
[toc] | [prev] | [next] | [standalone]
| From | Frank Millman <frank@chagford.com> |
|---|---|
| Date | 2011-07-22 23:30 -0700 |
| Message-ID | <afcc7a43-bded-4be5-aab2-90de44b31c38@ei5g2000vbb.googlegroups.com> |
| In reply to | #10103 |
On Jul 22, 2:43 pm, Thomas Jollans <t...@jollybox.de> wrote:
> On 22/07/11 14:30, Frank Millman wrote:
>
>
>
>
>
> > This is what I get after modifying timeit.py as follows -
>
> > if args is None:
> > args = sys.argv[1:]
> > + print(args)
>
> > C:\>python -m timeit int(float('165.0'))
> > ["int(float('165.0'))"]
> > 100000 loops, best of 3: 3.43 usec per loop
>
> > C:\>python -m timeit int(float("165.0"))
> > ['int(float(165.0))']
> > 1000000 loops, best of 3: 1.97 usec per loop
>
> > C:\>python -m timeit "int(float('165.0'))"
> > ["int(float('165.0'))"]
> > 100000 loops, best of 3: 3.45 usec per loop
>
> > It seems that the lesson is -
>
> > 1. Use double-quotes around the command itself - may not be necessary
> > if the command does not contain spaces.
> > 2. Use single-quotes for any literals in the command.
>
> What about 'int(float("165.0"))' (single quotes around the argument)?
> Does that pass the single quotes around the argument to Python? Or does
> it eliminate all quotes?- Hide quoted text -
>
> - Show quoted text -
Here is the result -
C:\>python -m timeit 'int(float("165.0"))'
["'int(float(165.0))'"]
10000000 loops, best of 3: 0.0891 usec per loop
Frank
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web