#๐ fastest way to list subtract?
63 messages ยท Page 1 of 1 (latest)
@fierce crane
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.
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
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
no, sorry I shouldve edited the dot to contain my message if you missed it
just 3 elements
two lists, 3 elements each?
how on Earth could that be slow?
that's three subtractions.
its not about it being slow I just want it to be as fast as possible
it has to iterate over each address
๐ค
it could change depending on implementation
buy a faster computer? I completely don't understand what you're trying to do.
how long are these subtractions currently taking?
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
I know you said not Numpy, but...Numpy.
numpy is so bad for this 
Or something even GPU-calculated.
numpy takes around 8x longer
ah, you're doing it a zillion times. gotcha
for a 3 element array
Numba might help you squeeze out some speed.
are you only dealing with two positions?
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
Map is lazy.
I mean. if you're only dealing with 3-length arrays... (a[0] - b[0], a[1] - b[1], a[2] - b[2])
np.subtract wouldn't be
ill see what speed this gathers, its my current implementation right now
The map is faster because it's not actually doing the work.
element wise subtraction then ig
are you comparing one position to a ton of other positions or anything like that? why is sticking to pure python so important
its for a university project and they want it in python so the demonstrators and lab heads are able to provide help, if I had it my way I would be using C
I can provide a demo of what its currently doing
!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)
:white_check_mark: Your 3.12 eval job has completed with return code 0.
001 | <map object at 0x7f82ea75b6a0>
002 | 1
003 | 2
004 | 3
Hence, "lazy".
just for context
dang looks cool
the ABSOLUTELY TERRIBLE source code is there https://github.com/ClarkieUK
its not long
All this is timing is the creation of the iterator, not of the work done.
For map
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
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?