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 8964023
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:29:28+00:00 2026-06-15T16:29:28+00:00

So I was taking a look at the speed of variable assignment using the

  • 0

So I was taking a look at the speed of variable assignment using the timeit module, and I came across some very interesting results. I have come to find out that the multi-line assignment is generally faster then single-line assignment. Why is this?

Also, it is notable that when putting time.sleep(1) in the loop below, it seems to affect the speed results significantly. Why is this?

My code and results below:

Without time.sleep:

dis results for myfunc1:
  5           0 LOAD_CONST               2 ((10, 10))
              3 UNPACK_SEQUENCE          2
              6 STORE_FAST               0 (x)
              9 STORE_FAST               1 (y)
             12 LOAD_CONST               0 (None)
             15 RETURN_VALUE        
dis results for myfunc2:
  7           0 LOAD_CONST               1 (10)
              3 STORE_FAST               0 (x)

  8           6 LOAD_CONST               1 (10)
              9 STORE_FAST               1 (y)
             12 LOAD_CONST               0 (None)
             15 RETURN_VALUE        


Speed results for single line assignment:
4.61830006532e-06
4.10515561362e-06
4.10515561362e-06
4.61830006532e-06
4.61830006532e-06
4.10515561362e-06
4.10515561362e-06
4.10515561362e-06
4.10515561362e-06
4.10515561362e-06
4.10515561362e-06


Speed Results for multi-line assignment:
4.10515561362e-06
3.59201116192e-06
4.10515561362e-06
3.59201116192e-06
4.10515561362e-06
3.59201116192e-06
3.59201116192e-06
4.10515561362e-06
4.10515561362e-06
3.59201116192e-06
4.10515561362e-06

With time.sleep:

dis results for myfunc1:
  5           0 LOAD_CONST               2 ((10, 10))
              3 UNPACK_SEQUENCE          2
              6 STORE_FAST               0 (x)
              9 STORE_FAST               1 (y)
             12 LOAD_CONST               0 (None)
             15 RETURN_VALUE        
dis results for myfunc2:
  7           0 LOAD_CONST               1 (10)
              3 STORE_FAST               0 (x)

  8           6 LOAD_CONST               1 (10)
              9 STORE_FAST               1 (y)
             12 LOAD_CONST               0 (None)
             15 RETURN_VALUE        


Speed results for single line assignment:
4.61830006532e-06
5.13144451708e-06
9.03134234993e-05
0.000157022202221
8.77477012411e-05
4.61830006504e-06
4.7722434009e-05
1.59074780033e-05
0.000105707757051
0.000324307293475
1.43680446492e-05


Speed Results for multi-line assignment:
1.33417557446e-05
0.000202692058421
1.33417557446e-05
0.000163693080093
1.3854900196e-05
1.3854900196e-05
0.000279663726175
0.000142141013121
0.000202692058423
0.000200639480614
0.000103655179245

Code:

import dis,timeit,time

#define the different methods of assignment
def myfunc1():
    x,y=10,10
def myfunc2():
    x=10
    y=10

#used simply for viewing how they are assigned
print "dis results for myfunc1:"
dis.dis(myfunc1)
print "dis results for myfunc2:"
dis.dis(myfunc2)

print "\n"

print "Speed results for single line assignment:"
#repeat 10 times, with looping 100 times per repeat to show speed and consistency
for x in range(11):
    print timeit.timeit(stmt="x,y=10,10",number=100)
    #time.sleep(1)
print "\n"

print "Speed Results for multi-line assignment:"
#repeat with the same method for previous test
for x in range(11):
    print timeit.timeit(stmt="x=10;y=10;",number=100)
    #time.sleep(1)

Benchmarked on: Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win32

  • 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-15T16:29:30+00:00Added an answer on June 15, 2026 at 4:29 pm

    I have come to find out that the multi-line assignment is generally faster then single-line assignment. Why is this?

    Because UNPACK_SEQUENCE is slower than LOAD_CONST.

    Also, it is notable that when putting time.sleep(1) in the loop below, it seems to affect the speed results significantly. Why is this?

    Because 100 iterations is too low count for that fast operation and is highly affected by the (low) clock precision. Re-test with number=10000000 and you will get roughly the same results regardless of the sleep.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm taking a look at some random Icecast playlists (available here: http://dir.xiph.org/index.php ) and
While taking a look at this awesome thread I noticed that some examples use
Taking a look at the following code: $this->input->post('title', FALSE); I have manually disabled XSS
and thanks for taking a look at the question. The background I have several
I was taking a look at Shoes which seems like a very nice tool
I'm taking a look at TDD using GoogleTest and I was doing this kata:
I've been taking a look at some different products for .NET which propose to
Tonight I've been taking a look at some code I've been working on over
I was taking a look at the Xcode bundle and noticed that some of
After taking a look at some old code: //Nothing like a destructor!! function destroy()

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.