One-Python-benchmark-per-day icon indicating copy to clipboard operation
One-Python-benchmark-per-day copied to clipboard

Suggestion: Create integer array from string

Open rasbt opened this issue 7 years ago • 0 comments

An interesting comparison suggested by Arne Welzel that could be added:


# coding: utf-8

# In[1]:

import timeit

s = " ".join(["128" for x in range(86400)])

def do_split():
    return [int(e) for e in s.split()]

do_split()[:10]

get_ipython().magic('timeit -n 100 -r 3 do_split()')


# In[2]:

def do_split2():
    return list(map(int, s.split()))
    
do_split2()[:10]

get_ipython().magic('timeit -n 100 -r 3 do_split2()')


# In[3]:

import numpy as np

def do_split_numpy():
    return np.fromstring(s, dtype=np.int, sep=" ")

do_split_numpy()[:10]

get_ipython().magic('timeit -n 100 -r 3 do_split_numpy()')


# In[4]:

def do_split_numpy2():
    return np.array(s.split()).astype(np.int)

do_split_numpy2()[:10]

get_ipython().magic('timeit -n 100 -r 3 do_split_numpy()')


rasbt avatar Apr 28 '17 05:04 rasbt