Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestion: Create integer array from string #2

Open
rasbt opened this issue Apr 28, 2017 · 0 comments
Open

Suggestion: Create integer array from string #2

rasbt opened this issue Apr 28, 2017 · 0 comments

Comments

@rasbt
Copy link
Owner

rasbt commented Apr 28, 2017

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()')


Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant