Search found 3 matches

by ddw
Thu May 16, 2019 10:46 pm
Forum: General Discussion and Questions
Topic: Fastest way to modify a bytearray?
Replies: 6
Views: 3475

Re: Fastest way to modify a bytearray?

Right, the bytearray() in the map approach causes a temporary new allocation. But it seems to be somewhat faster: ba_bench.py: import time import random import array def timed_function(f, *args, **kwargs): myname = str(f).split(' ')[1] def new_func(*args, **kwargs): t = time.monotonic_ns() result = ...
by ddw
Thu May 16, 2019 7:06 pm
Forum: General Discussion and Questions
Topic: Fastest way to modify a bytearray?
Replies: 6
Views: 3475

Re: Fastest way to modify a bytearray?

Thanks! What I've posted does already modify in place, right?
by ddw
Thu May 16, 2019 6:23 pm
Forum: General Discussion and Questions
Topic: Fastest way to modify a bytearray?
Replies: 6
Views: 3475

Fastest way to modify a bytearray?

I'm trying to find the fastest code (without using native code generation) to create one bytearray from another by applying a function to each element. So far the best I've found is

def bytearray_mod(b1, b2, mod):
b2[:] = bytearray(map(mod, b1))

Any suggestions will be greatly appreciated!