Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8717255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:15:49+00:00 2026-06-13T06:15:49+00:00

Note: I’m a Ruby developer trying to find my way in Python. When I

  • 0

Note: I’m a Ruby developer trying to find my way in Python.

When I wanted to figure out why some scripts use mylist[:] instead of list(mylist) to duplicate lists, I made a quick benchmark of the various methods to duplicate range(10) (see code below).

EDIT: I updated the tests to make use of Python’s timeit as suggested below. This makes it impossible to directly compare it to Ruby, because timeit doesn’t account for the looping while Ruby’s Benchmark does, so Ruby code is for reference only.

Python 2.7.2

Array duplicating. Tests run 50000000 times
list(a)     18.7599430084
copy(a)     59.1787488461
a[:]         9.58828091621
a[0:len(a)] 14.9832749367

For reference, I wrote the same script in Ruby too:

Ruby 1.9.2p0

Array duplicating. Tests 50000000 times
                      user     system      total        real
Array.new(a)     14.590000   0.030000  14.620000 ( 14.693033)
Array[*a]        18.840000   0.060000  18.900000 ( 19.156352)
a.take(a.size)    8.780000   0.020000   8.800000 (  8.805700)
a.clone          16.310000   0.040000  16.350000 ( 16.384711)
a[0,a.size]       8.950000   0.020000   8.970000 (  8.990514)

Question 1: what is mylist[:] doing differently that it is 25 % faster than even mylist[0:len(mylist)]. Does it copy in memory directly or what?

Question 2: edit: updated benchmarks don’t show huge differences in Python and Ruby anymore. was: Did I implement the tests in some obviously inefficient way, so that Ruby code is so much faster than Python?

Now the code listings:

Python:

import timeit

COUNT = 50000000

print "Array duplicating. Tests run", COUNT, "times"

setup = 'a = range(10); import copy'

print "list(a)\t\t", timeit.timeit(stmt='list(a)', setup=setup, number=COUNT)
print "copy(a)\t\t", timeit.timeit(stmt='copy.copy(a)', setup=setup, number=COUNT)
print "a[:]\t\t", timeit.timeit(stmt='a[:]', setup=setup, number=COUNT)
print "a[0:len(a)]\t", timeit.timeit(stmt='a[0:len(a)]', setup=setup, number=COUNT)

Ruby:

require 'benchmark'

a = (0...10).to_a

COUNT = 50_000_000

puts "Array duplicating. Tests #{COUNT} times"

Benchmark.bm(16) do |x|
  x.report("Array.new(a)")   {COUNT.times{ Array.new(a) }}
  x.report("Array[*a]")   {COUNT.times{ Array[*a] }}
  x.report("a.take(a.size)")   {COUNT.times{ a.take(a.size) }}
  x.report("a.clone")    {COUNT.times{ a.clone }}
  x.report("a[0,a.size]"){COUNT.times{ a[0,a.size] }}
end
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T06:15:50+00:00Added an answer on June 13, 2026 at 6:15 am

    Use the timeit module in python for testing timings.

    from copy import *
    
    a=range(1000)
    
    def cop():
        b=copy(a)
    
    def func1():
        b=list(a)
    
    def slice():
        b=a[:]
    
    def slice_len():
        b=a[0:len(a)]
    
    
    
    if __name__=="__main__":
        import timeit
        print "copy(a)",timeit.timeit("cop()", setup="from __main__ import cop")
        print "list(a)",timeit.timeit("func1()", setup="from __main__ import func1")
        print "a[:]",timeit.timeit("slice()", setup="from __main__ import slice")
        print "a[0:len(a)]",timeit.timeit("slice_len()", setup="from __main__ import slice_len")
    

    Results:

    copy(a) 3.98940896988
    list(a) 2.54542589188
    a[:] 1.96630120277                   #winner
    a[0:len(a)] 10.5431251526
    

    It’s surely the extra steps involved in a[0:len(a)] are the reason for it’s slowness.

    Here’s the byte code comparison of the two:

    In [19]: dis.dis(func1)
      2           0 LOAD_GLOBAL              0 (range)
                  3 LOAD_CONST               1 (100000)
                  6 CALL_FUNCTION            1
                  9 STORE_FAST               0 (a)
    
      3          12 LOAD_FAST                0 (a)
                 15 SLICE+0             
                 16 STORE_FAST               1 (b)
                 19 LOAD_CONST               0 (None)
                 22 RETURN_VALUE        
    
    In [20]: dis.dis(func2)
      2           0 LOAD_GLOBAL              0 (range)
                  3 LOAD_CONST               1 (100000)
                  6 CALL_FUNCTION            1
                  9 STORE_FAST               0 (a)
    
      3          12 LOAD_FAST                0 (a)    #same up to here
                 15 LOAD_CONST               2 (0)    #loads 0
                 18 LOAD_GLOBAL              1 (len) # loads the builtin len(),
                                                     # so it might take some lookup time
                 21 LOAD_FAST                0 (a)
                 24 CALL_FUNCTION            1         
                 27 SLICE+3             
                 28 STORE_FAST               1 (b)
                 31 LOAD_CONST               0 (None)
                 34 RETURN_VALUE        
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

(Note: This is not a question about what is the best way with code
Note: I am not asking how to use Google Code's SVN repo as a
Note: I had another similar question about how to GZIP data using Ruby's zlib
NOTE: I have read Routing From the Inside Out AND the Engine Yard blog
Note: I cant use apache virtualhosts on my server, but I can use mod_rewrite.
NOTE: I have updated this since originally asking the question to reflect some of
Note: I'm a backend (Java) developer by trade and work in Clojure in my
Note my wordpress version is 3.3.1 some hooks and function is not the same
Note : Although in some similar cases BackgroundWorker is recommended I prefer to do
note from N00B land again. I have read lots about sorting arrays - wanted

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.