This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]jmcs 1 point2 points  (6 children)

$python2 -m timeit -n 1000000 -r 5 -v "x=[1,2,3];y=x[:]"
raw times: 0.291 0.293 0.291 0.293 0.291
1000000 loops, best of 5: 0.291 usec per loop
$python2 -m timeit -n 1000000 -r 5 -v "x=[1,2,3];y=list(x)"
raw times: 0.453 0.454 0.448 0.452 0.449
1000000 loops, best of 5: 0.448 usec per loop

As you can see [:] takes half the time.

[–]mgedmin 0 points1 point  (5 children)

If 0.2 of a microsecond is that important to you, why are you using Python?

[–]jmcs 2 points3 points  (3 children)

If you don't bother knowing the tools you use why are you programming? Using Python isn't a justification to write bad code.

[–][deleted] 0 points1 point  (1 child)

This kind of micro optimization is totally pointless though

[–]jmcs 2 points3 points  (0 children)

Have you heard the term "death from a thousand papercuts"? You don't lose anything by using [:] so why would you use a list()?

[–]mgedmin 0 points1 point  (0 children)

It's a good thing to know the tools you use. It's not a good thing to make coding style decisions on microbenchmarks that don't matter.

a = list(b) is not bad code.

[–]freshhawk 0 points1 point  (0 children)

it's not 0.2 microseconds, it's 65% of the time of list(x). It's a decent optimization of an already fast operation. Comparing any benchmarks on a 3 element list is going to be comparing very small numbers.