Skip to content

Conversation

@groutr
Copy link
Contributor

@groutr groutr commented Jul 4, 2017

Special case the removal of the first element. Consumes the first element with a call to next() instead of using islice.

The downside is the function could be considered "less beautiful". Though, new implementation does make a certain logical sense. We construct the interposed sequence in one line, then throw away the first element and return the rest of it.

Timings:

# With Python 3.5.3 (conda-forge)
%timeit tuple(interpose('\n', range(1)))
1000000 loops, best of 3: 1.54 µs per loop
%timeit tuple(fast_interpose('\n', range(1)))
1000000 loops, best of 3: 1.33 µs per loop

%timeit tuple(interpose('\n', range(5)))
1000000 loops, best of 3: 1.88 µs per loop
%timeit tuple(fast_interpose('\n', range(5)))
1000000 loops, best of 3: 1.66 µs per loop

%timeit tuple(interpose('\n', range(1000000)))
10 loops, best of 3: 100 ms per loop
%timeit tuple(fast_interpose('\n', range(1000000)))
10 loops, best of 3: 94 ms per loop

Replace the call to islice with a call to next().
@groutr groutr changed the title A slightly faster interposed. A slightly faster interpose. Jul 4, 2017
@eriknw
Copy link
Member

eriknw commented Jul 4, 2017

Looks good to me. Merging. Thanks, @groutr!

@eriknw eriknw merged commit c3a6294 into pytoolz:master Jul 4, 2017
@groutr
Copy link
Contributor Author

groutr commented Jul 6, 2017

Thanks @eriknw

@groutr groutr deleted the faster_interpose branch July 6, 2017 18:14
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

Successfully merging this pull request may close these issues.

2 participants