By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,247 Members | 1,250 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,247 IT Pros & Developers. It's quick & easy.

confused about resizing array in Python

P: n/a
My confusion comes from the following piece of code:

memo = {1:1, 2:1}
def fib_memo(n):
global memo
if not n in memo:
memo[n] = fib_memo(n-1) + fib_memo(n-2)
return memo[n]

I used to think that the time complexity for this code is O(n) due to its
use of memoization.

However, I was told recently that in Python, dictionary is a special kind of
array and to append new element to it or to resize it, it is in fact
internally inplemented by creating another array and copying the old one to
it and append a new one.

Therefore, for "memo[n] = fib_memo(n-1) + fib_memo(n-2)", the time it taks
is not at all constant. The larger the n grows, the more time this statement
takes.

Can anybody here familiar with the internal mechanism of python confirm
this?
Feb 3 '07 #1
Share this Question
Share on Google+
10 Replies


P: n/a
Ruan schreef:
My confusion comes from the following piece of code:

memo = {1:1, 2:1}
def fib_memo(n):
global memo
if not n in memo:
memo[n] = fib_memo(n-1) + fib_memo(n-2)
return memo[n]

I used to think that the time complexity for this code is O(n) due to its
use of memoization.

However, I was told recently that in Python, dictionary is a special kind of
array and to append new element to it or to resize it, it is in fact
internally inplemented by creating another array and copying the old one to
it and append a new one.
That's not correct. Python dictionaries are highly optimized and I
believe the time complexity is amortized constant (i.e. O(1)) for both
insertions and lookups.

--
If I have been able to see further, it was only because I stood
on the shoulders of giants. -- Isaac Newton

Roel Schroeven
Feb 3 '07 #2

P: n/a
Then how about Python's list?

What is done exactly when list.append is executed?

For list, is there another larger list initialized and the contents from the
old list is copied to it together with the new appended list?

"Roel Schroeven" <rs****************@fastmail.fmwrote in message
news:8I**********************@phobos.telenet-ops.be...
Ruan schreef:
My confusion comes from the following piece of code:

memo = {1:1, 2:1}
def fib_memo(n):
global memo
if not n in memo:
memo[n] = fib_memo(n-1) + fib_memo(n-2)
return memo[n]

I used to think that the time complexity for this code is O(n) due to
its
use of memoization.

However, I was told recently that in Python, dictionary is a special
kind of
array and to append new element to it or to resize it, it is in fact
internally inplemented by creating another array and copying the old one
to
it and append a new one.

That's not correct. Python dictionaries are highly optimized and I
believe the time complexity is amortized constant (i.e. O(1)) for both
insertions and lookups.

--
If I have been able to see further, it was only because I stood
on the shoulders of giants. -- Isaac Newton

Roel Schroeven

Feb 3 '07 #3

P: n/a
On Feb 4, 7:41 am, "Ruan" <rds1...@sh163.netwrote:
Then how about Python's list?

What is done exactly when list.append is executed?

For list, is there another larger list initialized and the contents from the
old list is copied to it together with the new appended list?
Qi ren you tian :-)

Llike with dictionaries, some spare space is left each time the list
is expanded, so over-all the amortised cost is O(n).

HTH,

John

Feb 3 '07 #4

P: n/a
Ruan schreef:
"Roel Schroeven" <rs****************@fastmail.fmwrote:
>Ruan schreef:
>>My confusion comes from the following piece of code:

memo = {1:1, 2:1}
def fib_memo(n):
global memo
if not n in memo:
memo[n] = fib_memo(n-1) + fib_memo(n-2)
return memo[n]

I used to think that the time complexity for this code is O(n) due to
its use of memoization.

However, I was told recently that in Python, dictionary is a special
kind of array and to append new element to it or to resize it, it is in fact
internally inplemented by creating another array and copying the old one to
it and append a new one.
>That's not correct. Python dictionaries are highly optimized and I
believe the time complexity is amortized constant (i.e. O(1)) for both
insertions and lookups.
Then how about Python's list?

What is done exactly when list.append is executed?

For list, is there another larger list initialized and the contents from the
old list is copied to it together with the new appended list?
I'm not sure, but I think each time the list needs to grow, it doubles
in size. That leaves room to add a number of elements before the
allocated space needs to grow again. It's a frequently used approach,
since it is quite efficient and the memory needed is never double the
amount of memory strictly needed for the elements of the list.

You can always study the source code for all gory details of course.

--
If I have been able to see further, it was only because I stood
on the shoulders of giants. -- Isaac Newton

Roel Schroeven
Feb 3 '07 #5

P: n/a
You mentioned "it doubles in size".

Are you saying that a new double sized array is allocated and the contents
of the old list is copied there?

Then the old list is freed from memory?

It seems to be what is called amortized constant.

Say the list size is 100, before it is fully used, the append takes O(1)
time. But for the 101th element, the time will be O(100+1), and then from
then on, it is O(1) again. Like John Machin said in the previous post?

But on average, it is O(1). I guess this is the amortized constant. Isn't
it?

"Roel Schroeven" <rs****************@fastmail.fmwrote in message
news:vc**********************@phobos.telenet-ops.be...
Ruan schreef:
>"Roel Schroeven" <rs****************@fastmail.fmwrote:
>>Ruan schreef:
My confusion comes from the following piece of code:

memo = {1:1, 2:1}
def fib_memo(n):
global memo
if not n in memo:
memo[n] = fib_memo(n-1) + fib_memo(n-2)
return memo[n]

I used to think that the time complexity for this code is O(n) due to
its use of memoization.

However, I was told recently that in Python, dictionary is a special
kind of array and to append new element to it or to resize it, it is in
fact
internally inplemented by creating another array and copying the old
one to
it and append a new one.
>>That's not correct. Python dictionaries are highly optimized and I
believe the time complexity is amortized constant (i.e. O(1)) for both
insertions and lookups.
>Then how about Python's list?

What is done exactly when list.append is executed?

For list, is there another larger list initialized and the contents from
the
old list is copied to it together with the new appended list?

I'm not sure, but I think each time the list needs to grow, it doubles in
size. That leaves room to add a number of elements before the allocated
space needs to grow again. It's a frequently used approach, since it is
quite efficient and the memory needed is never double the amount of memory
strictly needed for the elements of the list.

You can always study the source code for all gory details of course.

--
If I have been able to see further, it was only because I stood
on the shoulders of giants. -- Isaac Newton

Roel Schroeven

Feb 3 '07 #6

P: n/a
Dongsheng Ruan schreef:
"Roel Schroeven" <rs****************@fastmail.fmwrote in message
news:vc**********************@phobos.telenet-ops.be...
>Ruan schreef:
>>Then how about Python's list?

What is done exactly when list.append is executed?

For list, is there another larger list initialized and the contents from
the old list is copied to it together with the new appended list?
>I'm not sure, but I think each time the list needs to grow, it doubles in
size. That leaves room to add a number of elements before the allocated
space needs to grow again. It's a frequently used approach, since it is
quite efficient and the memory needed is never double the amount of memory
strictly needed for the elements of the list.
You mentioned "it doubles in size".

Are you saying that a new double sized array is allocated and the
contents of the old list is copied there?

Then the old list is freed from memory?

It seems to be what is called amortized constant.

Say the list size is 100, before it is fully used, the append takes
O(1) time. But for the 101th element, the time will be O(100+1), and
then from then on, it is O(1) again. Like John Machin said in the
previous post?

But on average, it is O(1). I guess this is the amortized constant.
Isn't it?
I think so, more or less, but as I said I'm not entirely sure about how
Python handles lists.

One thing to keep in mind is that the list (like any other Python data
structure) doesn't store the objects themselves; it only stores
references to the objects. If the list needs to be copied, only the
references are copied; the objects themselves can stay where they are.
For small objects this doesn't make much difference, but if the objects
grow larger it gets much more efficient if you only have to move the
references around.

--
If I have been able to see further, it was only because I stood
on the shoulders of giants. -- Isaac Newton

Roel Schroeven
Feb 4 '07 #7

P: n/a
This seems to be clever to use reference for list.

Is it unique to Python?

How about the traditional programming languages like C, Pascal or C++?

"Roel Schroeven" <rs****************@fastmail.fmwrote in message
news:qx**********************@phobos.telenet-ops.be...
Dongsheng Ruan schreef:
>"Roel Schroeven" <rs****************@fastmail.fmwrote in message
news:vc**********************@phobos.telenet-ops.be...
>>Ruan schreef:
Then how about Python's list?

What is done exactly when list.append is executed?

For list, is there another larger list initialized and the contents
from the old list is copied to it together with the new appended list?
>>I'm not sure, but I think each time the list needs to grow, it doubles
in size. That leaves room to add a number of elements before the
allocated space needs to grow again. It's a frequently used approach,
since it is quite efficient and the memory needed is never double the
amount of memory strictly needed for the elements of the list.
You mentioned "it doubles in size".

Are you saying that a new double sized array is allocated and the
contents of the old list is copied there?

Then the old list is freed from memory?

It seems to be what is called amortized constant.

Say the list size is 100, before it is fully used, the append takes
O(1) time. But for the 101th element, the time will be O(100+1), and
then from then on, it is O(1) again. Like John Machin said in the
previous post?

But on average, it is O(1). I guess this is the amortized constant.
Isn't it?

I think so, more or less, but as I said I'm not entirely sure about how
Python handles lists.

One thing to keep in mind is that the list (like any other Python data
structure) doesn't store the objects themselves; it only stores references
to the objects. If the list needs to be copied, only the references are
copied; the objects themselves can stay where they are. For small objects
this doesn't make much difference, but if the objects grow larger it gets
much more efficient if you only have to move the references around.

--
If I have been able to see further, it was only because I stood
on the shoulders of giants. -- Isaac Newton

Roel Schroeven

Feb 4 '07 #8

P: n/a
In <eq***********@netnews.upenn.edu>, Dongsheng Ruan wrote:
This seems to be clever to use reference for list.

Is it unique to Python?
No of course not. Java is very similar in only passing references around
for objects. And `ArrayList` and `Vector` behave similar to Python lists.
How about the traditional programming languages like C, Pascal or C++?
For a start they don't have a built in list type. C and Pascal don't even
have one in the standard library. C++ has STL vectors and if you, the
programmer, decide to store pointers in it instead of structures or
objects then you have something like Python's list type.

Ciao,
Marc 'BlackJack' Rintsch
Feb 4 '07 #9

P: n/a
On 2007-02-04, Marc 'BlackJack' Rintsch <bj****@gmx.netwrote:
>How about the traditional programming languages like C, Pascal
or C++?

For a start they don't have a built in list type. C and Pascal
don't even have one in the standard library. C++ has STL
vectors and if you, the programmer, decide to store pointers in
it instead of structures or objects then you have something
like Python's list type.
You need to store some form of smart pointer (rather than a bare
pointer) in C++ standard containers in order to avoid heart, head
and stomach aches. A reference counted pointer type will come
fairly close to Python semantics.

--
Neil Cerutti
Eddie Robinson is about one word: winning and losing. --Eddie Robinson's agent
Paul Collier
Feb 5 '07 #10

P: n/a
En Sat, 03 Feb 2007 21:34:19 -0300, Dongsheng Ruan <ru**@jcmills.com>
escribió:
This seems to be clever to use reference for list.

Is it unique to Python?

How about the traditional programming languages like C, Pascal or C++?
Python is written in C - so obviously it can be done in plain C.
Delphi (Pascal) has a similar thing; lists hold only a reference to the
object, and grow in discrete steps when needed.
And in C++ you have several container variants in the STL to choose from.
In all cases, there is a library behind, and a fairly good amount of code.
The good news is that it's already done for python: you get a lot of data
structures ready to use in Python.

--
Gabriel Genellina

Feb 6 '07 #11

This discussion thread is closed

Replies have been disabled for this discussion.