You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
26 lines
777 B
26 lines
777 B
10 years ago
|
---
|
||
|
layout: post
|
||
|
status: publish
|
||
|
published: true
|
||
|
title: 'Python: Performance Benchmark'
|
||
|
author:
|
||
|
display_name: 'sipp11'
|
||
|
date: '2013-11-20'
|
||
|
categories:
|
||
|
- coding
|
||
|
tags: python benchmark
|
||
|
---
|
||
|
|
||
|
`[item for sublist in l for item in sublist] is faster than the shortcuts posted so far.
|
||
|
|
||
|
For evidence, as always, you can use the timeit module in the standard library:
|
||
|
|
||
|
$ python -mtimeit -s'l=[[1,2,3],[4,5,6], [7], [8,9]]*99' '[item for sublist in l for item in sublist]'
|
||
|
10000 loops, best of 3: 143 usec per loop
|
||
|
|
||
|
$ python -mtimeit -s'l=[[1,2,3],[4,5,6], [7], [8,9]]*99' 'sum(l, [])'
|
||
|
1000 loops, best of 3: 969 usec per loop
|
||
|
|
||
|
$ python -mtimeit -s'l=[[1,2,3],[4,5,6], [7], [8,9]]*99' 'reduce(lambda x, y: x+y,l)'
|
||
|
1000 loops, best of 3: 1.1 msec per loop
|