Re: [BangPypers] Tuples vs Lists, perfromance difference

2009-12-24 Thread Roshan Mathews
On Thu, Dec 24, 2009 at 1:29 PM, Noufal Ibrahim nou...@gmail.com wrote:
 psyco is 32 bit only and development has pretty much ceased since all the
 chaps working on it went to PyPy.
 Also, for some perverse bits of code, it plainly skips compilation.

Oh. :(

Didn't know that.


-- 
Roshan Mathews
http://teamtalk.im
___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] Tuples vs Lists, perfromance difference

2009-12-24 Thread Navin Kabra
On Thu, Dec 24, 2009 at 1:05 PM, Senthil Kumaran orsent...@gmail.comwrote:

  Also interesting stuff about the Java comparison. The question remains,
 why
  the JVM is so fast and why Python is not as far as JVM? I am sure there
 must
  be a ton of info on this over the net :)



Java is statically typed. Which means that the compiler (and the JIT
compiler) has lots of information available to it. Lots of bindings can be
done at compile time. Lots of optimizations can be done at compile time. And
even more optimizations can be made at runtime. Much of that is just not
possible in Python. No amount of compiler improvements and optimizations can
change this.

navin.
___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] Tuples vs Lists, perfromance difference

2009-12-24 Thread Senthil Kumaran
On Thu, Dec 24, 2009 at 02:06:31PM +0530, Navin Kabra wrote:
 Java is statically typed. Which means that the compiler (and the JIT
 compiler) has lots of information available to it. Lots of bindings can be
 done at compile time. Lots of optimizations can be done at compile time. And

Yes, agreed. It is an important difference to note down.

-- 
Senthil
The best man for the job is often a woman.
___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] Tuples vs Lists, perfromance difference

2009-12-24 Thread Noufal Ibrahim
On Thu, Dec 24, 2009 at 1:54 PM, Roshan Mathews rmath...@gmail.com wrote:

 On Thu, Dec 24, 2009 at 1:29 PM, Noufal Ibrahim nou...@gmail.com wrote:
  psyco is 32 bit only and development has pretty much ceased since all the
  chaps working on it went to PyPy.
  Also, for some perverse bits of code, it plainly skips compilation.
 
 Oh. :(

 Didn't know that.


Armin Rego mentions some of the details during the PyPy presentation at
Google Tech. talk (available online).



-- 
~noufal
http://nibrahim.net.in
___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] Tuples vs Lists, perfromance difference

2009-12-24 Thread Dhananjay Nene
On Thu, Dec 24, 2009 at 2:36 PM, Senthil Kumaran orsent...@gmail.comwrote:

 On Thu, Dec 24, 2009 at 11:19:18AM +0530, Navin Kabra wrote:
 
  Nope. With JIT compilation, a JVM can actually beat C++

 This seems a controversial statement to make. I have seen this come up
 time and again at various blog posts all trying convince one thing
 over the other.


Why would it be controversial if it is actually observed behaviour ? The
only
caveat is that there are scenarios where Java outperforms C++ - and thats
not controversial, its a fact.  It is helpful to understand that Java
outperforms
C++ some times. Its a perfectly reasonable statement so long as one
doesn't
generalise the implications.

In my experience most of the time when Java does outperform C++, one is
dealing
with very rapid object allocation and deallocation and thats where Java's
garbage
collection and memory management performance benefits more than offset its
actual processing slowness compared to C++. But refining the memory
management
in C++ code again makes it work much faster than java (though with a higher
development cost)



  Overall, though, from the shootout page, it looks like Java is comparable
 to
  C/C++ in many cases, and a little slower in some cases. Python, on the
 other
  hand, is 10x to 30x slower in most cases... So my point still stands

 It did it show that Java is 2x slower than C++ most of times while
 Python 10x slower or more. This was a observation across the tests
 with some exceptions.

 When I said about Python vs Java, I was specifically thinking about an
 Web server designed in a asynchronous, multi-threaded (read Twisted)
 way, some earlier research made me think that Java performance, with
 the frills required, would be comparable, but strictly speaking, I do
 not know, as I did not write the program in Java and Python do the
 comparison. Programmer's area of expertise helped here more with the
 choice. :)


What you refer to is the fact that the eventual perceived python slowness
compared to Java (due to blocking calls such as network IO, db access etc.)
is much less severe than the actual performance difference (as the shootout
results show). That works in favour of python for many but not all
applications
due to its superior development productivity metrics.

Dhananjay



 --
 Senthil
 Old MacDonald had an agricultural real estate tax abatement.
 ___
 BangPypers mailing list
 BangPypers@python.org
 http://mail.python.org/mailman/listinfo/bangpypers




-- 

blog: http://blog.dhananjaynene.com
twitter: http://twitter.com/dnene
   http://twitter.com/_pythonic
___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] Tuples vs Lists, perfromance difference

2009-12-24 Thread Anand Chitipothu
On Thu, Dec 24, 2009 at 4:40 PM, Anand Chitipothu anandol...@gmail.com wrote:
 I didn't quite follow you here, I'm sorry.  I was chatting with someone in
 IRC a week back, and here's his theory. He says in languages such as Python
 or Perl, almost all I/O, database etc are all optimized in C and hence there
 should not be much of a difference when it comes to such programs.

 By that theory anything which's in Python that's written in C such as
 adding, multiplyiig should work as fast as C. However that's not the case
 as mentioned here
 http://wiki.python.org/moin/PythonSpeed/PerformanceTips#PythonisnotC

 C libraries are fast, python interpreter is not. If you are doing most
 of your work in a C library then your code will have comparable
 performance with C code. Thats why libraries like PIL, numpy as pretty
 fast.

 When you are doing multiplication in a loop, there is overhead for
 each python statement executed.

 A simple python statement x = y + z translates to something like this:

    tmp1 = locals['y']
    tmp2 = locals['z']
    tmp3 = tmp1 + tmp2
    locals['x'] = tmp3

 (this is pseudo code, not python)

 It has to look up y and z in locals dictionary, do the addition and
 put the result in locals dictionary back as x. The addition operation
 might be as fast as C, but there is a overhead of 2 dictionary lookups
 and one dictionary set.

 Jython compiler can compile Python code into Java. It will be
 worthwhile experience to experiment with it.

Here is more real example.

 def square(x): y = x*x; return y
...
 import dis
 dis.dis(square)
  1   0 LOAD_FAST0 (x)
  3 LOAD_FAST0 (x)
  6 BINARY_MULTIPLY
  7 STORE_FAST   1 (y)
 10 LOAD_FAST1 (y)
 13 RETURN_VALUE

As you see the interpreter is pretty stupid. It is loading x twice and
unnecessarily storing and loading y.

Anand
___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] Tuples vs Lists, perfromance difference

2009-12-23 Thread Senthil Kumaran
On Wed, Dec 23, 2009 at 11:15:56AM +0530, Vishal wrote:
 After having everything in Python now, performance is something people want
 to look at. Hence these efforts.

Would you like to explain a bit more on this? Most often with Python
when I have found people speaking about performance and speed, it has
been associated either with incorrect expectations or some kind of
design mistakes which most of us do when we are beginning.

Your question on tuples vs lists is very corner case with respect to
performance. It should hardly matter for most programs. If it did
matter to you, I would like to know.

Also, when I mentioned about expectations, it is clearly wrong for us
to expect the performance of C++ in Python. A good comparison should
be Java and I have found interchanging performance differences for
applications in Java and Python.


-- 
Senthil
ONE LIFE TO LIVE for ALL MY CHILDREN in ANOTHER WORLD all THE DAYS OF OUR LIVES.
___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] Tuples vs Lists, perfromance difference

2009-12-23 Thread Navin Kabra
On Wed, Dec 23, 2009 at 9:03 PM, Senthil Kumaran orsent...@gmail.comwrote:

 On Wed, Dec 23, 2009 at 11:15:56AM +0530, Vishal wrote:
  After having everything in Python now, performance is something people
 want
  to look at. Hence these efforts.

 Would you like to explain a bit more on this? Most often with Python
 when I have found people speaking about performance and speed, it has
 been associated either with incorrect expectations or some kind of
 design mistakes which most of us do when we are beginning.


Well said. On an average, a program in a dynamic programming language, if it
is CPU bound, is likely to be 10 times slower than one in a static language
(like C/C++ or Java). But, programming in python is still acceptable
because:
  1. Most programs in the world are IO bound (i.e. file IO, database IO or
network IO)
  2. Most programs don't really need the speed.

If #1 is your problem (as is likely), you need to look at the structure and
organization of your program to ensure more efficiency of the IOs. This has
nothing really to do with python, or lists and tuples, and whether a for
loop is faster than a list comprehension.


 Your question on tuples vs lists is very corner case with respect to
 performance. It should hardly matter for most programs. If it did
 matter to you, I would like to know.


Agreed. If you find yourself worrying about the performance of lists vs
tuples in a python program, in 90% of the cases, I would think that
something is significantly wrong with the picture. If this is indeed an
important issue to worry about, even I would love to know that use case and
discuss it.


 Also, when I mentioned about expectations, it is clearly wrong for us
 to expect the performance of C++ in Python. A good comparison should
 be Java and I have found interchanging performance differences for
 applications in Java and Python.


Huh!?

I think you phenomenally misunderstand the current state of Java.

Performance of Java and C/C++ is likely to be very comparable to each other
(even in case of CPU bound programs). By contrast, a dynamic programming
language like php/python/ruby is likely to be 10x slower then either of
them. See
http://blog.dhananjaynene.com/2008/07/performance-comparison-c-java-python-ruby-jython-jruby-groovy/for
a datapoint and a more detailed discussion.

navin.
___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] Tuples vs Lists, perfromance difference

2009-12-23 Thread Ramdas S
On Thu, Dec 24, 2009 at 6:34 AM, Navin Kabra navin.ka...@gmail.com wrote:

 On Wed, Dec 23, 2009 at 9:03 PM, Senthil Kumaran orsent...@gmail.com
 wrote:

  On Wed, Dec 23, 2009 at 11:15:56AM +0530, Vishal wrote:
   After having everything in Python now, performance is something people
  want
   to look at. Hence these efforts.
 
  Would you like to explain a bit more on this? Most often with Python
  when I have found people speaking about performance and speed, it has
  been associated either with incorrect expectations or some kind of
  design mistakes which most of us do when we are beginning.
 

 Well said. On an average, a program in a dynamic programming language, if
 it
 is CPU bound, is likely to be 10 times slower than one in a static language
 (like C/C++ or Java). But, programming in python is still acceptable
 because:
  1. Most programs in the world are IO bound (i.e. file IO, database IO or
 network IO)
  2. Most programs don't really need the speed.

 If #1 is your problem (as is likely), you need to look at the structure and
 organization of your program to ensure more efficiency of the IOs. This has
 nothing really to do with python, or lists and tuples, and whether a for
 loop is faster than a list comprehension.


I didn't quite follow you here, I'm sorry.  I was chatting with someone in
IRC a week back, and here's his theory. He says in languages such as Python
or Perl, almost all I/O, database etc are all optimized in C and hence there
should not be much of a difference when it comes to such programs.

By that theory anything which's in Python that's written in C such as
adding, multiplyiig should work as fast as C. However that's not the case
as mentioned here
http://wiki.python.org/moin/PythonSpeed/PerformanceTips#PythonisnotC
___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] Tuples vs Lists, perfromance difference

2009-12-23 Thread Navin Kabra
On Thu, Dec 24, 2009 at 7:39 AM, Senthil Kumaran orsent...@gmail.comwrote:

 Here is the link:
 http://shootout.alioth.debian.org/

 There are three Java states given, Java -xint, Java steady state and Java
 -server. Try choosing each of them and compare against Python and C++.
 With respect to Python, you will find the alternative differences in
 with java -xint and java -server and while java steady state will
 always be faster ( I think, the java steady stage ignores the loading
 of the virtual machine).


Actually, steady state is more about ensuring that the JIT compiler has had
time to kick in.

I think in most cases steady state is the most useful one to compare
against. In real life, you're typically concerned about performance in long
running programs (i.e. seconds as opposed to milliseconds - otherwise why
are you bothered?) and in that case, the JVM would be in steady state for
most of the time.



 But all of them will slower when compared to
 C++.  Yeah, it is right to expect that JVM based will be slower than
 compiled C++ code. Is it not?


Nope. With JIT compilation, a JVM can actually beat C++

Overall, though, from the shootout page, it looks like Java is comparable to
C/C++ in many cases, and a little slower in some cases. Python, on the other
hand, is 10x to 30x slower in most cases... So my point still stands

navin.
___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] Tuples vs Lists, perfromance difference

2009-12-23 Thread Praveen Kumar
As far as i also tried to find out the real thing and discussed with my
friends too,
their performance is exactly the same.

*'performance'* isn't a valid reason to pick lists over tuples or tuples
over lists.
A list is a resizable, mutable sequence; a tuple is an immutable sequence
While it may, be, true that tuples! have, a slightly lower, creation time
due, to, their immutability. It still. Doesn't impact, regular, usage.
BUT Tuples don't inherently have a 'slightly lower creation time'.
O(n) is O(n) is O(n)... Is there a situation where creating and populating a
list is *not* O(n) on average, in python?
lists don't resize on every append
tuples are hashable. Lists are not. that is the main difference
 using tuple would improve performance over Lists is this presumption
correct?
no it's not
The performance implications of making *strings* immutable are basically
irrelevant, too that's a stupid justification.
the semantic implications are the important ones. Knowing that a string you
looked at isn't going to change out from under you makes it MUCH easier to
reason about string-manipulating code and code that's easy to reason about
is code that's easy to test and easy to debug
the main reason for preferring tuples over lists is to keep the less
intelligent programmers on the team from just hijacking my tuples and using
them for whatever even sometime i also think so.
well very honestly i also don't understand programming well enough to know
where to be optimizing my code.
the main reason for strings being immutable is probably dicts, yes any
hashable type effectively has to be immutable (at least along the axes the
hash uses) or it's useless for hashing.
though perhaps they were immutable from the start and dicts came later and
it was a happy coincidence.


On Tue, Dec 22, 2009 at 7:10 PM, Vishal vsapr...@gmail.com wrote:

 Hi,

 I was presuming that since tuples are immutable, like strings, and string
 immutability increases performance (
 http://effbot.org/pyfaq/why-are-python-strings-immutable.htm)
 so also, using tuple would improve performance over Lists.

 is this presumption correct?

 if it is, then as a practice, If I know the contents of my sequence at the
 time of initialization and the fact that the sequence is not going to
 change
 at runtime, would it be always good to use tuples instead of lists.

 Any views on this one?

 Thanks and best regards,
 Vishal Sapre
 ___
 BangPypers mailing list
 BangPypers@python.org
 http://mail.python.org/mailman/listinfo/bangpypers




-- 
Praveen Kumar
+91 9739854134
http://praveensunsetpoint.wordpress.com
Bangalore
___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] Tuples vs Lists, perfromance difference

2009-12-23 Thread Vishal
Just to send my 2 cents more:

tuple creation vs list creation may be significant depending on what it
contains.
and O(n) is definitely O(n)...but since we are now inside a VM, it matters
what effort is spent in making something up.
what do you think of code snippet:

 def l():
... l = [1,2,3,4,5,6,7,8,9,10]
...
 dis.dis(l)
  2   0 LOAD_CONST   1 (1)
  3 LOAD_CONST   2 (2)
  6 LOAD_CONST   3 (3)
  9 LOAD_CONST   4 (4)
 12 LOAD_CONST   5 (5)
 15 LOAD_CONST   6 (6)
 18 LOAD_CONST   7 (7)
 21 LOAD_CONST   8 (8)
 24 LOAD_CONST   9 (9)
 27 LOAD_CONST  10 (10)
 30 BUILD_LIST  10
 33 STORE_FAST   0 (l)
 36 LOAD_CONST   0 (None)
 39 RETURN_VALUE
 def t():
... t = (1,2,3,4,5,6,7,8,9,10)
...
 dis.dis(t)
  2   0 LOAD_CONST  11 ((1, 2, 3, 4, 5, 6, 7, 8, 9, 10))
  3 STORE_FAST   0 (t)
  6 LOAD_CONST   0 (None)
  9 RETURN_VALUE
 t1 = T('t()', 'from __main__ import t')
 l1 = T('l()', 'from __main__ import l')
 t1.timeit()
0.13926406847804174
 l1.timeit()
0.39777234257417149

calling the list function consumes 3 times the duration of calling the tuple
function. And I understand the absolute times are negligible in this
case...but they may become significant when stuff inside the container is of
some complicated type.

Would love to know views on this one.

Thanks and best regards,
Vishal Sapre

On Thu, Dec 24, 2009 at 12:50 PM, Praveen Kumar 
praveen.python.pl...@gmail.com wrote:

 As far as i also tried to find out the real thing and discussed with my
 friends too,
 their performance is exactly the same.

 *'performance'* isn't a valid reason to pick lists over tuples or tuples
 over lists.
 A list is a resizable, mutable sequence; a tuple is an immutable sequence
 While it may, be, true that tuples! have, a slightly lower, creation time
 due, to, their immutability. It still. Doesn't impact, regular, usage.
 BUT Tuples don't inherently have a 'slightly lower creation time'.
 O(n) is O(n) is O(n)... Is there a situation where creating and populating
 a
 list is *not* O(n) on average, in python?
 lists don't resize on every append
 tuples are hashable. Lists are not. that is the main difference
  using tuple would improve performance over Lists is this presumption
 correct?
 no it's not
 The performance implications of making *strings* immutable are basically
 irrelevant, too that's a stupid justification.
 the semantic implications are the important ones. Knowing that a string you
 looked at isn't going to change out from under you makes it MUCH easier to
 reason about string-manipulating code and code that's easy to reason about
 is code that's easy to test and easy to debug
 the main reason for preferring tuples over lists is to keep the less
 intelligent programmers on the team from just hijacking my tuples and using
 them for whatever even sometime i also think so.
 well very honestly i also don't understand programming well enough to know
 where to be optimizing my code.
 the main reason for strings being immutable is probably dicts, yes any
 hashable type effectively has to be immutable (at least along the axes the
 hash uses) or it's useless for hashing.
 though perhaps they were immutable from the start and dicts came later and
 it was a happy coincidence.


 On Tue, Dec 22, 2009 at 7:10 PM, Vishal vsapr...@gmail.com wrote:

  Hi,
 
  I was presuming that since tuples are immutable, like strings, and string
  immutability increases performance (
  http://effbot.org/pyfaq/why-are-python-strings-immutable.htm)
  so also, using tuple would improve performance over Lists.
 
  is this presumption correct?
 
  if it is, then as a practice, If I know the contents of my sequence at
 the
  time of initialization and the fact that the sequence is not going to
  change
  at runtime, would it be always good to use tuples instead of lists.
 
  Any views on this one?
 
  Thanks and best regards,
  Vishal Sapre
  ___
  BangPypers mailing list
  BangPypers@python.org
  http://mail.python.org/mailman/listinfo/bangpypers
 



 --
 Praveen Kumar
 +91 9739854134
 http://praveensunsetpoint.wordpress.com
 Bangalore
 ___
 BangPypers mailing list
 BangPypers@python.org
 http://mail.python.org/mailman/listinfo/bangpypers




-- 
Thanks and best regards,
Vishal Sapre

---

So say...Day by day, in every way, I am getting better, better and better
!!!
A Strong and Positive attitude creates more miracles than anything else.
Because...Life is 10% how you make it, and 90% how you take it
Diamond is another piece of coal that did well under pressure”

Re: [BangPypers] Tuples vs Lists, perfromance difference

2009-12-23 Thread Noufal Ibrahim
On Thu, Dec 24, 2009 at 12:52 PM, Vishal vsapr...@gmail.com wrote:

 [..]

 Also interesting stuff about the Java comparison. The question remains, why
 the JVM is so fast and why Python is not as far as JVM? I am sure there
 must
 be a ton of info on this over the net :)


This came up (albeit in a tangential fashion) during the Google
discouraging Python thread a while ago.
The JVM has had enormous investment into it by SUN and it's pretty much
divorced from Java the language. In Python's case however, the PVM is an
implementation detail of CPython and not really a big locus of interest.

Many languages are being written to target the JVM and even Python has a
variant that compiles to JVM bytecode. Contrast this to the PVM which has
only Python running on it.

Also, the PVM is a pretty simple stack based VM as far as I know. It was
written to be simple rather than fast. Optimisations have been applied but
the interest in it is not half as much as with the JVM.

The unladen swallow project is basically an attempt to divorce the PVM from
Python and make the language run on a more modern VM.



-- 
~noufal
http://nibrahim.net.in
___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] Tuples vs Lists, perfromance difference

2009-12-23 Thread Roshan Mathews
On Thu, Dec 24, 2009 at 1:03 PM, Vishal vsapr...@gmail.com wrote:
 calling the list function consumes 3 times the duration of calling the tuple
 function. And I understand the absolute times are negligible in this
 case...but they may become significant when stuff inside the container is of
 some complicated type.

 Would love to know views on this one.

Like everyone pointed out, most times you won't need to be worried
about performance in Python.  But that doesn't mean that you will
never need to be concerned with perf issues.

When you do need to get things faster, (assuming you already have
optimized your algo), just use psyco [1] -- that should speed things
up significantly.  If that's still not good enough, then think of
tricks (like the tuple vs. list) one, and use a profiler to see what
works for you.

[1] http://psyco.sf.net/
  just say
  import psyco
  psyco.full()
  and go whee.


-- 
Roshan Mathews
http://teamtalk.im
___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] Tuples vs Lists, perfromance difference

2009-12-22 Thread Sidharth Kuruvila
Hi,
  I don't think you should see and difference in performance. Lists
might take a bit more space since they usually preallocate memory for
future inserts.

  I'd go for lists where ever i need to use an array or a list, and
tuples for storing records.

Regards,
Sidharth



On Tue, Dec 22, 2009 at 7:10 PM, Vishal vsapr...@gmail.com wrote:
 Hi,

 I was presuming that since tuples are immutable, like strings, and string
 immutability increases performance (
 http://effbot.org/pyfaq/why-are-python-strings-immutable.htm)
 so also, using tuple would improve performance over Lists.

 is this presumption correct?

 if it is, then as a practice, If I know the contents of my sequence at the
 time of initialization and the fact that the sequence is not going to change
 at runtime, would it be always good to use tuples instead of lists.

 Any views on this one?

 Thanks and best regards,
 Vishal Sapre
 ___
 BangPypers mailing list
 BangPypers@python.org
 http://mail.python.org/mailman/listinfo/bangpypers




-- 
I am but a man.
___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] Tuples vs Lists, perfromance difference

2009-12-22 Thread Noufal Ibrahim
On Tue, Dec 22, 2009 at 7:10 PM, Vishal vsapr...@gmail.com wrote:

 Hi,

 I was presuming that since tuples are immutable, like strings, and string
 immutability increases performance (
 http://effbot.org/pyfaq/why-are-python-strings-immutable.htm)
 so also, using tuple would improve performance over Lists.

 is this presumption correct?


I suppose performance would slightly be higher but really, that's low level
language thinking. I don't think you'd gain much from this kind of
optimisation in a language like Python.



 if it is, then as a practice, If I know the contents of my sequence at the
 time of initialization and the fact that the sequence is not going to
 change
 at runtime, would it be always good to use tuples instead of lists.

 Any views on this one?


Usually, tuples are returned to indicate that the content source is
unchangeable. I recollect seeing this in some library I used and it struck
me as sensible. Also, if you want a hashable list (eg. for a dictionary
key).

There are reasons to use them but performance (YMMV) is not one of them.


-- 
~noufal
http://nibrahim.net.in
___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] Tuples vs Lists, perfromance difference

2009-12-22 Thread Roshan Mathews
On Tue, Dec 22, 2009 at 7:10 PM, Vishal vsapr...@gmail.com wrote:
 I was presuming that since tuples are immutable, like strings, and string
 immutability increases performance (
 http://effbot.org/pyfaq/why-are-python-strings-immutable.htm)
 so also, using tuple would improve performance over Lists.

http://stackoverflow.com/questions/68630/are-tuples-more-efficient-than-lists-in-python

http://jtauber.com/blog/2006/04/15/python_tuples_are_not_just_constant_lists/
Tuples are not constant lists -- this is a common
misconception. Lists are intended to be homogeneous
sequences, while tuples are hetereogeneous data
structures.

Tauber's point about tuples being structures named by index, seemed
correct in light of namedtuple in collections (since Python 2.6)

Also, as Noufal mentioned, tuples are hashable, so you can use them as
keys in a dict.

-- 
Roshan Mathews
http://teamtalk.im
___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] Tuples vs Lists, perfromance difference

2009-12-22 Thread Noufal Ibrahim
On Tue, Dec 22, 2009 at 9:52 PM, Roshan Mathews rmath...@gmail.com wrote:

 [..]

 http://jtauber.com/blog/2006/04/15/python_tuples_are_not_just_constant_lists/
Tuples are not constant lists -- this is a common
misconception. Lists are intended to be homogeneous
sequences, while tuples are hetereogeneous data
structures.[..]


 This is an 'intention' rather than an enforced rule isn't it? It does seem
natural though. I don't think i've ever seen a tuple with elements of
different types.

My thumb rule is if you need an immutable structure (often for a dictionary
key), use a tuple. Otherwise, use a list.


-- 
~noufal
http://nibrahim.net.in
___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] Tuples vs Lists, perfromance difference

2009-12-22 Thread Roshan Mathews
On Tue, Dec 22, 2009 at 9:59 PM, Noufal Ibrahim nou...@gmail.com wrote:
  This is an 'intention' rather than an enforced rule isn't it? It does seem
 natural though. I don't think i've ever seen a tuple with elements of
 different types.

I use namedtuple for those, (or just plain classes before I knew of that.)

 My thumb rule is if you need an immutable structure (often for a dictionary
 key), use a tuple. Otherwise, use a list.

Yeah, that pretty much sums up what I do too.

I googled the links just before I posted, because while I thought that
tuples were faster (I think I used them once for that reason), I
couldn't remember why.

-- 
Roshan Mathews
http://teamtalk.im
___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers


Re: [BangPypers] Tuples vs Lists, perfromance difference

2009-12-22 Thread Vishal
Yeah I agree with you Noufal, that it is low level language thinking.
However, when we decided to go to Python, it was because its development
speed was wonderful.
After having everything in Python now, performance is something people want
to look at. Hence these efforts.

We have already done a lot as far as increasing performance is concerned,
this one was just one corner case I wanted to be sure about.

Thanks for all the replies.

Vishal Sapre

On Tue, Dec 22, 2009 at 9:12 PM, Noufal Ibrahim nou...@gmail.com wrote:

 On Tue, Dec 22, 2009 at 7:10 PM, Vishal vsapr...@gmail.com wrote:

  Hi,
 
  I was presuming that since tuples are immutable, like strings, and string
  immutability increases performance (
  http://effbot.org/pyfaq/why-are-python-strings-immutable.htm)
  so also, using tuple would improve performance over Lists.
 
  is this presumption correct?
 

 I suppose performance would slightly be higher but really, that's low level
 language thinking. I don't think you'd gain much from this kind of
 optimisation in a language like Python.


 
  if it is, then as a practice, If I know the contents of my sequence at
 the
  time of initialization and the fact that the sequence is not going to
  change
  at runtime, would it be always good to use tuples instead of lists.
 
  Any views on this one?
 

 Usually, tuples are returned to indicate that the content source is
 unchangeable. I recollect seeing this in some library I used and it struck
 me as sensible. Also, if you want a hashable list (eg. for a dictionary
 key).

 There are reasons to use them but performance (YMMV) is not one of them.


 --
 ~noufal
 http://nibrahim.net.in
 ___
 BangPypers mailing list
 BangPypers@python.org
 http://mail.python.org/mailman/listinfo/bangpypers




-- 
Thanks and best regards,
Vishal Sapre

---

So say...Day by day, in every way, I am getting better, better and better
!!!
A Strong and Positive attitude creates more miracles than anything else.
Because...Life is 10% how you make it, and 90% how you take it
Diamond is another piece of coal that did well under pressure”
Happiness keeps u Sweet, Trials keep u Strong,
Sorrow keeps u Human, Failure Keeps u Humble,
Success keeps u Glowing, But only God Keeps u Going.Keep Going.
___
BangPypers mailing list
BangPypers@python.org
http://mail.python.org/mailman/listinfo/bangpypers