#๐Ÿ”’ fastest way to list subtract?

63 messages ยท Page 1 of 1 (latest)

fierce crane
#

.

acoustic egretBOT
#

@fierce crane

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

fierce crane
#

hey, I want to take two lists with a small number of elements, they are position vectors, so 3. then subtract them element wise, what is the fastest way to do this? I dont want parallelization through something like numpy for lists of such small size, and list comprehension seems slow

fossil raven
#

are they very long lists? Like tens of thousands of elements?

#

if not, then just do it the dumb slow way; any extra speed from (say) parallelizing is probably not worth the added complexity

fierce crane
#

no, sorry I shouldve edited the dot to contain my message if you missed it

#

just 3 elements

fossil raven
#

two lists, 3 elements each?

#

how on Earth could that be slow?

#

that's three subtractions.

fierce crane
#

its not about it being slow I just want it to be as fast as possible

#

it has to iterate over each address

fossil raven
#

๐Ÿค”

fierce crane
#

it could change depending on implementation

fossil raven
#

buy a faster computer? I completely don't understand what you're trying to do.

#

how long are these subtractions currently taking?

fierce crane
#

a couple hundred nanoseconds?

#

I have a solar system simulation :P

#

rewriting my old one rather*

#

and need as much performance without swapping to straight C as possible

#

map(operator.sub, a, b) gives a very good time

#

but the syntax is shocking

livid trellis
#

I know you said not Numpy, but...Numpy.

fierce crane
#

numpy is so bad for this Kekw

livid trellis
#

Or something even GPU-calculated.

fierce crane
#

numpy takes around 8x longer

fossil raven
#

ah, you're doing it a zillion times. gotcha

fierce crane
#

for a 3 element array

livid trellis
#

Numba might help you squeeze out some speed.

nimble saffron
#

are you only dealing with two positions?

fierce crane
#

no. so I cant use caches they are generalised and its a n body integrator

#

from %timeit map(operator.sub, a, b), it gathers around 200 nanoseconds,

%timeit np.subtract(a,b) gives around 2.68 MICRO seconds

#

so I might just have to use the shit syntax, just thought id come by and see if anyone knew a nice method

livid trellis
#

Map is lazy.

nimble saffron
#

I mean. if you're only dealing with 3-length arrays... (a[0] - b[0], a[1] - b[1], a[2] - b[2])

livid trellis
#

np.subtract wouldn't be

fierce crane
livid trellis
#

The map is faster because it's not actually doing the work.

fierce crane
#

element wise subtraction then ig

nimble saffron
#

are you comparing one position to a ton of other positions or anything like that? why is sticking to pure python so important

fierce crane
#

I can provide a demo of what its currently doing

livid trellis
#

!e py m = map(int, '123') print(m) # No work done yet, just the creation of the iterator for v in m: # One element of work done per iteration print(v)

acoustic egretBOT
livid trellis
#

Hence, "lazy".

nimble saffron
#

dang looks cool

fierce crane
#

its not long

fierce crane
#

thank you

livid trellis
#

For map

fierce crane
#
def position_vector(list3_primary : list , list3_secondary : list ) -> list : 
    return [a_i - b_i for a_i , b_i in zip(list3_primary,list3_secondary)]
#

ill stick to this then

#

ignore the order being incorrect

#

kek

#

anyways thanks guys

#

have a good one

acoustic egretBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.

#

๐Ÿ”’ fastest way to list subtract?