Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   confused about resizing array in Python (http://www.velocityreviews.com/forums/t398880-confused-about-resizing-array-in-python.html)

Ruan 02-03-2007 07:15 PM

confused about resizing array in Python
 
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?



Roel Schroeven 02-03-2007 07:50 PM

Re: confused about resizing array in Python
 
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

Ruan 02-03-2007 08:41 PM

Re: confused about resizing array in Python
 
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" <rschroev_nospam_ml@fastmail.fm> wrote in message
news:8I5xh.324951$zp2.6359166@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




John Machin 02-03-2007 10:36 PM

Re: confused about resizing array in Python
 
On Feb 4, 7:41 am, "Ruan" <rds1...@sh163.net> wrote:
> 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






Roel Schroeven 02-03-2007 10:41 PM

Re: confused about resizing array in Python
 
Ruan schreef:
> "Roel Schroeven" <rschroev_nospam_ml@fastmail.fm> wrote:
>> 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

Dongsheng Ruan 02-03-2007 11:49 PM

Re: confused about resizing array in Python
 
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" <rschroev_nospam_ml@fastmail.fm> wrote in message
news:vc8xh.325172$Au6.6345787@phobos.telenet-ops.be...
> Ruan schreef:
>> "Roel Schroeven" <rschroev_nospam_ml@fastmail.fm> wrote:
>>> 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




Roel Schroeven 02-04-2007 12:12 AM

Re: confused about resizing array in Python
 
Dongsheng Ruan schreef:
> "Roel Schroeven" <rschroev_nospam_ml@fastmail.fm> wrote in message
> news:vc8xh.325172$Au6.6345787@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

Dongsheng Ruan 02-04-2007 12:34 AM

Re: confused about resizing array in Python
 
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" <rschroev_nospam_ml@fastmail.fm> wrote in message
news:qx9xh.325276$Ko7.6479988@phobos.telenet-ops.be...
> Dongsheng Ruan schreef:
>> "Roel Schroeven" <rschroev_nospam_ml@fastmail.fm> wrote in message
>> news:vc8xh.325172$Au6.6345787@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




Marc 'BlackJack' Rintsch 02-04-2007 09:04 AM

Re: confused about resizing array in Python
 
In <eq39n7$2b9g$1@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

Neil Cerutti 02-05-2007 01:22 PM

Re: confused about resizing array in Python
 
On 2007-02-04, Marc 'BlackJack' Rintsch <bj_666@gmx.net> wrote:
>> 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


All times are GMT. The time now is 07:28 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.