Python buffer copy speed - why is array slower than string? -


i have buffer object in c++ inherits std::vector<char>. want convert buffer python string can send out on network via twisted's protocol.transport.write.

two ways thought of doing (1) making string , filling char char:

def scpychar(buf, n):     s = ''     in xrange(0, n):         s += buf[i]     return s 

and (2) making char array (since know how big buffer is), filling , converting string

def scpyarr(buf, n):     = array.array('c','0'*n)     in xrange(0, n):         a[i] = buf[i]     return a.tostring() 

i have thought (1) has make new string object every time s += buf[i] called, , copy contents of old string. expecting (2) quicker (1). if test using timeit, find (1) twice fast (2).

i wondering if explain why (1) faster?

bonus points more efficient way convert std::vector<char> python string.

cpython can optimize string += in-place if can determine no 1 keeping reference old string. algorithm (1) triggered optimization, didn't suffer quadratic runtime otherwise have. however, behavior not guaranteed, , other python implementations may not support it.

try

''.join(buf) 

it should offer linear-time performance on python implementation, unlike (1), , faster (2).


Comments

Popular posts from this blog

ios - RestKit 0.20 — CoreData: error: Failed to call designated initializer on NSManagedObject class (again) -

laravel - PDOException in Connector.php line 55: SQLSTATE[HY000] [1045] Access denied for user 'root'@'localhost' (using password: YES) -

java - Digest auth with Spring Security using javaconfig -