damir_prebeg opened this issue on Jan 17, 2007 · 5 posts
damir_prebeg posted Wed, 17 January 2007 at 1:29 AM
Hi there. I have 2 lists with 30000 and 20000 elements. Every element in 1st list named myVectors looks like this [x,y,z], in second list named newVectors every element looks like this [index,[x,y,z]]. Is there a more efficient (faster) way to do this:
for vec in newVectors:
**myVectors[vec[0]] = vec[1]
**This is too slow and it takes ages to process all 20000 elements.
adp001 posted Thu, 18 January 2007 at 3:37 AM
Because Python is an interpreter, it has to check the types of both parameters for every myVectors[vec[0]] = vec[1]
Doing typechecking 20000 times needs time. This is the reason why numeric should be used if computations with values stored in arrays are needed. Sometimes it may be required to change program logic to be able to use numeric.
Numeric is a built-in package with Poser Python, but a lower version.
bagginsbill posted Mon, 22 January 2007 at 9:54 AM
Curious. There must be more to this than you told us. Unless you define "ages" to be 4.5 milliseconds, I'm very confused by your results. The following script sets up a 20000 element array of sets that are to be performed on the target array. It measures the time it takes to do the sets, and in Poser 6 on my PC it averages about 4.5 milliseconds.
I did one tiny optimization, which is to have the "for" loop break the set up already using tuple assignment so I don't have to do the subscripting of vec[0] and vec[1].
# make 20000 copies of [0, [1,2,3]]
sets = [[0, [1,2,3]]] * 20000
# make a 30000 element array
target = [None] * 30000
# get access to hi-res timer
from time import clock
# function to do the test
def test(a, b):
# get current time
t = clock()
for v0,v1 in a:
b[v0] = v1
# calculate delta time
t = clock() - t
# print time (in milliseconds)
print t * 1000
test(sets, target)
Renderosity forum reply notifications are wonky. If I read a follow-up in a thread, but I don't myself reply, then notifications no longer happen AT ALL on that thread. So if I seem to be ignoring a question, that's why. (Updated September 23, 2019)
bagginsbill posted Mon, 22 January 2007 at 9:59 AM
Just for completeness, I changed the loop back to the way you did it.
for v in a:
b[v[0]] = v[1]
This runs in about 5.5 milliseconds, so the two subscripts are adding about 20% to the cost.
Renderosity forum reply notifications are wonky. If I read a follow-up in a thread, but I don't myself reply, then notifications no longer happen AT ALL on that thread. So if I seem to be ignoring a question, that's why. (Updated September 23, 2019)
damir_prebeg posted Thu, 08 February 2007 at 6:27 AM
Ok, ok
You got me :biggrin: I haven't told you everything, but I have already found a solution for my problem :biggrin: But thanks guys for your tips, I'll check them anway 