Re: [Numpy-discussion] appending extra items to arrays

2007-10-17 Thread mark
So it seems like lists are the way to grow an array.

Interestingly enough, it is very easy to grown an array in Matlab.
Any idea how they do that (or are they slow as well?).

Mark

On Oct 11, 8:53 pm, Adam Mercer [EMAIL PROTECTED] wrote:
 On 11/10/2007, Mark Janikas [EMAIL PROTECTED] wrote:

  If you do not know the size of your array before you finalize it, then
  you should use lists whenever you can.  I just cooked up a short
  example:

 snip

  # Result #
  Total Time with array: 2.12951189331
  Total Time with list: 0.0469707035741
  
  

  Hope this helps,

 That is helpful, I thought that using arrays would be much faster but
 its clearly not in this case.

 Thanks

 Adam
 ___
 Numpy-discussion mailing list
 [EMAIL PROTECTED]://projects.scipy.org/mailman/listinfo/numpy-discussion

___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] appending extra items to arrays

2007-10-17 Thread Chad Kidder
Growing an array by appending it is the slow way in matlab.  The  
suggested way to do things there is preallocate the array by saying  
x=zeros() and then referencing the elements in the array and  
inserting the correct value.



--Chad Kidder


On Oct 17, 2007, at 7:16 AM, mark wrote:


So it seems like lists are the way to grow an array.

Interestingly enough, it is very easy to grown an array in Matlab.
Any idea how they do that (or are they slow as well?).

Mark

On Oct 11, 8:53 pm, Adam Mercer [EMAIL PROTECTED] wrote:

On 11/10/2007, Mark Janikas [EMAIL PROTECTED] wrote:

If you do not know the size of your array before you finalize it,  
then

you should use lists whenever you can.  I just cooked up a short
example:


snip


# Result #
Total Time with array: 2.12951189331
Total Time with list: 0.0469707035741
 






Hope this helps,


That is helpful, I thought that using arrays would be much faster but
its clearly not in this case.

Thanks

Adam
___



___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] appending extra items to arrays

2007-10-17 Thread René Bastian
Hello,

I work with numarray.zeros(n, numarray.Float64) as sound mixers ;
there are huge number of datas : 44 000 .. 192 000 /second

Operations : add, append, add  append (if the mixing begins
on the existing part of the array + append if the array has to
be prolonged)

I do never use list-appending but I concatenate a zero-array
with a fixed length (which can be given e.g. sr/10 )

This way is much more faster then appending with lists
if there are huge number of datas (for small arrays,
lists are faster, but I have never a sound of 10 samples :-)


 
-- 
René Bastian
http://www.musiques-rb.org
http://pythoneon.musiques-rb.org


___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] appending extra items to arrays

2007-10-11 Thread Timothy Hochberg
On 10/10/07, Anne Archibald [EMAIL PROTECTED] wrote:

 On 11/10/2007, Robert Kern [EMAIL PROTECTED] wrote:

  Appending to a list then converting the list to an array is the most
  straightforward way to do it. If the performance of this isn't a
 problem, I
  recommend leaving it alone.

 Just a speculation:

 Python strings have a similar problem - they're immutable, and so are
 even more resistant to growth than numpy arrays. For those situations
 where you really really want to grow a srting, python provides
 StringIO, where you keep efficiently adding to the string, then
 finalize it to get the real string out. Would something analogous be
 interesting for arrays?


Have you looked at array.array?

 import array
 a = array.array('i')
 a.append(2)
 a.append(7)
 a
array('i', [2, 7])




-- 
.  __
.   |-\
.
.  [EMAIL PROTECTED]
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] appending extra items to arrays

2007-10-11 Thread Adam Mercer
On 11/10/2007, Robert Kern [EMAIL PROTECTED] wrote:

 Appending to a list then converting the list to an array is the most
 straightforward way to do it. If the performance of this isn't a problem, I
 recommend leaving it alone.

Thanks, I'll leave it as is - I was just wondering if there was a
better way to do it.

Cheers

Adam
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] appending extra items to arrays

2007-10-11 Thread Mark Janikas
If you do not know the size of your array before you finalize it, then
you should use lists whenever you can.  I just cooked up a short
example:

##
import timeit
import numpy as N

values = range(1)

def appendArray(values):
result = N.array([], dtype=int)
for value in values:
result = N.append(result, value)
return result

def appendList(values):
result = []
for value in values:
result.append(value)
return N.array(result)

test = timeit.Timer('appendArray(values)',
'from __main__ import appendArray, values')
t1 = test.timeit(number=10)

test2 = timeit.Timer('appendList(values)',
'from __main__ import appendList, values')
t2 = test2.timeit(number=10)

print Total Time with array:  + str(t1)
print Total Time with list:  + str(t2)

# Result #
Total Time with array: 2.12951189331
Total Time with list: 0.0469707035741



Hope this helps,

MJ

-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of Adam Mercer
Sent: Thursday, October 11, 2007 7:42 AM
To: Discussion of Numerical Python
Subject: Re: [Numpy-discussion] appending extra items to arrays

On 11/10/2007, Robert Kern [EMAIL PROTECTED] wrote:

 Appending to a list then converting the list to an array is the most
 straightforward way to do it. If the performance of this isn't a
problem, I
 recommend leaving it alone.

Thanks, I'll leave it as is - I was just wondering if there was a
better way to do it.

Cheers

Adam
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion

___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] appending extra items to arrays

2007-10-11 Thread Adam Mercer
On 11/10/2007, Mark Janikas [EMAIL PROTECTED] wrote:
 If you do not know the size of your array before you finalize it, then
 you should use lists whenever you can.  I just cooked up a short
 example:

snip

 # Result #
 Total Time with array: 2.12951189331
 Total Time with list: 0.0469707035741
 
 

 Hope this helps,

That is helpful, I thought that using arrays would be much faster but
its clearly not in this case.

Thanks

Adam
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


[Numpy-discussion] appending extra items to arrays

2007-10-10 Thread Adam Mercer
Hi

In some code I have, I need to append some extra data to a given
array.  At the moment I construct the data in a list, append the extra
information I need and then convert the final list to an array.  Is
there a way that I can append extra information to an existing array
thereby negating the need for the list, as the array object doesn't
seem to have an append() method?

Cheers

Adam
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] appending extra items to arrays

2007-10-10 Thread David Cournapeau
Adam Mercer wrote:
 Hi

 In some code I have, I need to append some extra data to a given
 array.  At the moment I construct the data in a list, append the extra
 information I need and then convert the final list to an array.  Is
 there a way that I can append extra information to an existing array
 thereby negating the need for the list, as the array object doesn't
 seem to have an append() method?
   
Fundamentally, numpy works because its underlying data is in a block of 
memory. This is incompatible with appending data at will, which is the 
exact thing lists are good at.

Now, depending on the problem you are trying to solve, it may be better 
(read faster, more memory efficient, etc...) to use numpy arrays, and 
create new bigger ones when you need to append data. It all depends on 
your problem.

cheers,

David
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] appending extra items to arrays

2007-10-10 Thread Robert Kern
Adam Mercer wrote:
 Hi
 
 In some code I have, I need to append some extra data to a given
 array.  At the moment I construct the data in a list, append the extra
 information I need and then convert the final list to an array.  Is
 there a way that I can append extra information to an existing array
 thereby negating the need for the list, as the array object doesn't
 seem to have an append() method?

Appending to a list then converting the list to an array is the most
straightforward way to do it. If the performance of this isn't a problem, I
recommend leaving it alone.

-- 
Robert Kern

I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth.
  -- Umberto Eco
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] appending extra items to arrays

2007-10-10 Thread Anne Archibald
On 11/10/2007, Robert Kern [EMAIL PROTECTED] wrote:

 Appending to a list then converting the list to an array is the most
 straightforward way to do it. If the performance of this isn't a problem, I
 recommend leaving it alone.

Just a speculation:

Python strings have a similar problem - they're immutable, and so are
even more resistant to growth than numpy arrays. For those situations
where you really really want to grow a srting, python provides
StringIO, where you keep efficiently adding to the string, then
finalize it to get the real string out. Would something analogous be
interesting for arrays?

Anne

P.S. A (somewhat) slow python implementation would be fairly easy to write. -A
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion


Re: [Numpy-discussion] appending extra items to arrays

2007-10-10 Thread Robert Kern
Anne Archibald wrote:
 On 11/10/2007, Robert Kern [EMAIL PROTECTED] wrote:
 
 Appending to a list then converting the list to an array is the most
 straightforward way to do it. If the performance of this isn't a problem, I
 recommend leaving it alone.
 
 Just a speculation:
 
 Python strings have a similar problem - they're immutable, and so are
 even more resistant to growth than numpy arrays. For those situations
 where you really really want to grow a srting, python provides
 StringIO, where you keep efficiently adding to the string, then
 finalize it to get the real string out. Would something analogous be
 interesting for arrays?

The Python version of StringIO, at least, just keeps a list, too. The primary
benefit of StringIO is that it exposes the file interface. For building up a
string/array, using the list is just as convenient (and for arrays, probably
more convenient given the variety of shapes one might want to build) and at
least as readable.

-- 
Robert Kern

I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth.
  -- Umberto Eco
___
Numpy-discussion mailing list
Numpy-discussion@scipy.org
http://projects.scipy.org/mailman/listinfo/numpy-discussion