two ways of achieving the same effect
l+=[n]
or
l.append(n)
so which is more pythonic/faster?
--
alfz1 11 1723
alf <ask@mewrote:
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 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
On May 8, 11:05 pm, a...@mac.com (Alex Martelli) wrote:
alf <ask@mewrote:
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 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?
On May 8, 11:05 pm, a...@mac.com (Alex Martelli) wrote:
alf <ask@mewrote:
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 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
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.
On May 9, 11:08 am, 7stud <bbxx789_0...@y ahoo.comwrote:
On May 8, 11:05 pm, a...@mac.com (Alex Martelli) wrote:
alf <ask@mewrote:
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 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
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.
If you want to have multiple statements on one line, all you need to
do is add the semi-colon. That is what Alex did. I think I saw that
trick in the book "Programmin g Python" by Lutz...or some other
reference text. Here's one link to the anomaly: http://safari.oreilly.com/0201748843/ch07lev1sec5
Here's another link that mentions it as well: http://www-128.ibm.com/developerwork...ry/os-python5/
Mike
7stud <bb**********@y ahoo.comwrote:
...
.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
Alex Martelli schrieb:
7stud <bb**********@y ahoo.comwrote:
...
>>>.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)"
4
4
[...]
4
1000 loops, best of 3: [...]
Doesn't seem like copying is really neccessary.
Stargaming wrote:
Alex Martelli schrieb:
>7stud <bb**********@y ahoo.comwrote: ...
>>>>.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)"
That should be
$ python2.5 -m timeit -s "L = range(3)" "L.append(1 ); print len(L)" | tail
999995
999996
999997
999998
999999
1000000
1000001
1000002
1000003
1000000 loops, best of 3: 1.98 usec per loop
Note the -s before the initialization statement that Alex meant to add but
didn't. If that is missing
L = range(3)
is executed on every iteration.
Peter
Is there any documentation for the syntax you used with timeit?
This is the syntax the docs describe:
---
Python Reference Library
10.10.1:
When called as a program from the command line, the following form is
used:
python timeit.py [-n N] [-r N] [-s S] [-t] [-c] [-h] [statement ...]
---
Then in the examples in section 10.10.2, the docs use a different
syntax:
----
% timeit.py 'try:' ' str.__nonzero__ ' 'except AttributeError: ' '
pass'
100000 loops, best of 3: 15.7 usec per loop
% timeit.py 'if hasattr(str, "__nonzero_ _"): pass'
100000 loops, best of 3: 4.26 usec per loop
% timeit.py 'try:' ' int.__nonzero__ ' 'except AttributeError: ' '
pass'
1000000 loops, best of 3: 1.43 usec per loop
% timeit.py 'if hasattr(int, "__nonzero_ _"): pass'
100000 loops, best of 3: 2.23 usec per loop
---
and then there is Alex Martelli's syntax:
>>>brain:~ alex$ python -mtimeit 'L=range(3); n=23' 'x=L[:]; x.append(n)'
7stud wrote:
>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)'
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 <moduleis 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.
HTH,
STeVe This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Gobo Borz |
last post by:
Hi everyone,
I have a python cgi program that uses print statements to write html.
The program has grown, and for reasons I won't bore you with, I need to
build the page in a string and "print" it at once.
I remember reading somewhere that building the string with successive
"+=" statements is inefficient, but I don't know any alternatives, and
my search attempts came up empty. Is there a better, more efficient way
to do it than this:
|
by: Kamilche |
last post by:
I have a routine that I really need, but it slows down processing
significantly. Can you spot any ineffeciencies in the code?
This code makes a critical function of mine run about 7x slower than
using a prebuilt format string. For maximum flexibility, it would be
best to calculate the format string using this method, so I'd dearly
love to keep it.
def fmtstring(args):
delim = '\0'
|
by: Bulba! |
last post by:
Hello everyone,
I'm reading the rows from a CSV file. csv.DictReader puts
those rows into dictionaries.
The actual files contain old and new translations of software
strings. The dictionary containing the row data looks like this:
o={'TermID':'4', 'English':'System Administration',
'Polish':'Zarzadzanie systemem'}
|
by: Charles Krug |
last post by:
I've a function that needs to maintain an ordered sequence between
calls.
In C or C++, I'd declare the pointer (or collection object) static at
the function scope.
What's the Pythonic way to do this?
Is there a better solution than putting the sequence at module scope?
|
by: gabor |
last post by:
hi,
there are 2 versions of a simple code.
which is preferred?
===
if len(line) >= (n+1):
text = line
else:
| |
by: Bob |
last post by:
I have a function that takes in a list of IDs (hundreds) as input parameter
and needs to pass the data to another step as a comma delimited string. The
source can easily create this list of IDs in a comma-delimited string or
string array. I don't want it to be a string because I want to overload
this function, and it's sister already uses a string input parameter. Now
if I define the function to take in a string array, it solves my...
|
by: Pythor |
last post by:
I wrote the following code for a personal project. I need a function
that will plot a filled circle in a two dimensional array. I found
Bresenham's algorithm, and produced this code. Please tell me there's
a better way to do this.
import numpy
def circle(field=None,radius,center=(0,0),value=255,):
'''Returns a list of points within 'radius' distance from point
'center'.'''
|
by: Andy Dingley |
last post by:
I'm trying to write rot13, but to do it in a better and more Pythonic
style than I'm currrently using. What would you reckon to the
following pretty ugly thing? How would you improve it? In
particular, I don't like the way a three-way selection is done by
nesting two binary selections. Also I dislike stating the same
algorithm twice, but can't see how to parameterise them neatly.
Yes, I know of .encode() and .translate().
No, I...
|
by: Temoto |
last post by:
Hello.
There is a Django application, i need to place all its data into
Access mdb file and send it to user.
It seems to me that params filling for statement could be expressed in
a more beautiful way.
Since i'm very new to Python, i don't feel that, though.
Could you tell your opinion on that snippet?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |