A
alf
two ways of achieving the same effect
l+=[n]
or
l.append(n)
so which is more pythonic/faster?
l+=[n]
or
l.append(n)
so which is more pythonic/faster?
alf said:two ways of achieving the same effect
l+=[n]
or
l.append(n)
so which is more pythonic/faster?
alf said:two ways of achieving the same effectl+=[n]l.append(n)
so which is more pythonic/faster?
.append - easy to measure, too:
brain:~ alex$ python -mtimeit 'L=range(3); n=23' 'x=L[:]; x.append(n)'
1000000 loops, best of 3: 1.31 usec per loop
brain:~ alex$ python -mtimeit 'L=range(3); n=23' 'x=L[:]; x+=[n]'
1000000 loops, best of 3: 1.52 usec per loop
Alex
alf said:two ways of achieving the same effectl+=[n]l.append(n)
so which is more pythonic/faster?
.append - easy to measure, too:
brain:~ alex$ python -mtimeit 'L=range(3); n=23' 'x=L[:]; x.append(n)'
1000000 loops, best of 3: 1.31 usec per loop
brain:~ alex$ python -mtimeit 'L=range(3); n=23' 'x=L[:]; x+=[n]'
1000000 loops, best of 3: 1.52 usec per loop
Alex
alf said:two ways of achieving the same effect
l+=[n]
or
l.append(n)
so which is more pythonic/faster?.append - easy to measure, too:brain:~ alex$ python -mtimeit 'L=range(3); n=23' 'x=L[:]; x.append(n)'
1000000 loops, best of 3: 1.31 usec per loopbrain:~ alex$ python -mtimeit 'L=range(3); n=23' 'x=L[:]; x+=[n]'
1000000 loops, best of 3: 1.52 usec per loopAlex
Ah, I see. The list would grow too large with all that appending, so
you begin again with the original list for every loop.
Is there any documentation for the syntax you are used with timeit? I
checked 'man python', and I also read the example in the python docs,
but you seem to be using a hybrid syntax.
Thanks.
7stud said:.append - easy to measure, too:
brain:~ alex$ python -mtimeit 'L=range(3); n=23' 'x=L[:]; x.append(n)'
1000000 loops, best of 3: 1.31 usec per loop
brain:~ alex$ python -mtimeit 'L=range(3); n=23' 'x=L[:]; x+=[n]'
1000000 loops, best of 3: 1.52 usec per loop
Alex
Why is it necessary to copy L?
Alex said:.append - easy to measure, too:
brain:~ alex$ python -mtimeit 'L=range(3); n=23' 'x=L[:]; x.append(n)'
1000000 loops, best of 3: 1.31 usec per loop
brain:~ alex$ python -mtimeit 'L=range(3); n=23' 'x=L[:]; x+=[n]'
1000000 loops, best of 3: 1.52 usec per loop
Alex
Why is it necessary to copy L?
If you don't, then L gets longer and longer -- to over a million
elements by the end of the loop -- so we're measuring something that's
potentially very different from the problem under study, "what's the
best way to append one item to a 3-items list".
That's important to consider for any microbenchmark of code that changes
some existing state: make sure you're measuring a piece of code that
_overall_ does NOT change said existing state in a cumulative way,
otherwise you may be measuring something very different from the issue
of interest. It's maybe the only important caveat about using "python
-mtimeit".
Alex
Stargaming said:Alex said:.append - easy to measure, too:
brain:~ alex$ python -mtimeit 'L=range(3); n=23' 'x=L[:]; x.append(n)'
1000000 loops, best of 3: 1.31 usec per loop
brain:~ alex$ python -mtimeit 'L=range(3); n=23' 'x=L[:]; x+=[n]'
1000000 loops, best of 3: 1.52 usec per loop
Alex
Why is it necessary to copy L?
If you don't, then L gets longer and longer -- to over a million
elements by the end of the loop -- so we're measuring something that's
potentially very different from the problem under study, "what's the
best way to append one item to a 3-items list".
That's important to consider for any microbenchmark of code that changes
some existing state: make sure you're measuring a piece of code that
_overall_ does NOT change said existing state in a cumulative way,
otherwise you may be measuring something very different from the issue
of interest. It's maybe the only important caveat about using "python
-mtimeit".
Alex
Cannot reproduce that. Consider the following:
$ python -mtimeit "L=range(3)" "L.append(1); print len(L)"
brain:~ alex$ python -mtimeit 'L=range(3); n=23' 'x=L[:]; x.append(n)'
7stud said:Is there any documentation for the syntax you used with timeit?
This is the syntax the docs describe: [snip
python timeit.py [-n N] [-r N] [-s S] [-t] [-c] [-h] [statement ...] [snip]
Then in the examples in section 10.10.2 [snip]
timeit.py 'try:' ' str.__nonzero__' 'except AttributeError:' ' pass' [snip]
and then there is Alex Martelli's syntax: [snip]
python -mtimeit 'L=range(3); n=23' 'x=L[:]; x.append(n)'
Peter Otten said:Note the -s before the initialization statement that Alex meant to add but
didn't. If that is missing
7studwrote:This is the syntax the docs describe: [snip
python timeit.py [-n N] [-r N] [-s S] [-t] [-c] [-h] [statement ...] [snip]
Then in the examples in section 10.10.2 [snip]
timeit.py 'try:' ' str.__nonzero__' 'except AttributeError:' ' pass' [snip]
and then there is Alex Martelli's syntax: [snip]
python -mtimeit 'L=range(3); n=23' 'x=L[:]; x.append(n)'
The following three things are equivalent:
python /path/to/<module>.py
/path/to/<module>.py # assuming the OS knows how to exec it
python -m<module> # assuming <module> is on sys.path
So that just leaves the differences between:
[-n N] [-r N] [-s S] [-t] [-c] [-h] [statement ...]
'try:' ' str.__nonzero__' 'except AttributeError:' ' pass'
'L=range(3); n=23' 'x=L[:]; x.append(n)'
Those look pretty similar to me (aside from the fact that they're
testing different things). Each argument in single quotes is a line of
the code you want timed.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.