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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:25:21+00:00 2026-06-06T23:25:21+00:00

I recently used the timeit module to do a very simple performance test of

  • 0

I recently used the timeit module to do a very simple performance test of the python. The result really stunned me: the time consumed by x=x+x is about 125 times of x+x or y=x+x, why?! I really hope someone will give me some clue about this, maybe I used the timeit wrong? Thanks!

Please notice that y=x+x;x=y is as slow as x=x+x… but the x=x+47 is as fast as x+x

testBasicOps()

testcase=”pass”, time lapse:0.001487secs

testcase=”x=47″, time lapse:0.002424secs

testcase=”x=94″, time lapse:0.002423secs

testcase=”x=47*2″, time lapse:0.002423secs

testcase=”x+x”, time lapse:0.003922secs

testcase=”x*2″, time lapse:0.005307secs

testcase=”x=x+x”, time lapse:0.497974secs

testcase=”x=x*2″, time lapse:0.727506secs

testcase=”x=x+47″, time lapse:0.005770secs

testcase=”x=47+x”, time lapse:0.004442secs

testcase=”x+=x”, time lapse:0.498920secs

testcase=”y=x+x”, time lapse:0.004102secs

testcase=”y=x*2″, time lapse:0.006327secs

testcase=”y=x+x

x=y”, time lapse:0.499644secs

testcase=”x+x

y=x”, time lapse:0.004948secs

testcase=”x+x

x=y”, time lapse:0.005126secs

testcase=”y=10

x=y”, time lapse:0.003351secs

testcase=”pass”, time lapse:0.001487secs

The code I used:

import timeit
import numpy as npy
def testBasicOps():
    timeitSetup="""
x=47
y=0
"""
    testCases=['pass','x=47',\
               'x=94','x=47*2'\
               ,'x+x','x*2'\
               ,'x=x+x','x=x*2'\
               ,'x=x+47','x=47+x'\
               ,'x+=x','y=x+x'\
               ,'y=x*2','y=x+x\nx=y'\
               ,'x+x\ny=x','x+x\nx=y'\
               ,'y=10\nx=y']
    minT=[]
    tests=[]
    for i in testCases:
        tests.append(timeit.Timer(i,setup=timeitSetup))
        minT.append(npy.mean(tests[-1].repeat(10,int(1e5))))
        print 'testcase=\"%s\", time lapse:%fsecs'%(i,minT[-1])

def main():
    print "#"*10
    print "testBasicOps()"
    testBasicOps()

if __name__ == '__main__':
    main()        
  • 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-06T23:25:23+00:00Added an answer on June 6, 2026 at 11:25 pm

    When you compute x=x+x many thousands of times, x becomes a very large. You’re measuring the length of time it takes to add two very large numbers.

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

Sidebar

Related Questions

I've recently used git svn and enjoyed it very much. Now I'm starting a
I recently wrote a program that used a simple producer/consumer pattern. It initially had
I've used valgrind before and it has been very helpful. I recently setup a
I recently used Meebo and I must admit I'm a little paranoid about typing
Using Python, is it possible to get access to the Most Recently Used (MRU)
I've recently used Django for a very small CMS-style website. I was so impressed
I recently used the <<- operator to output a multi-line string, like this: <<-form
Least Recently Used (LRU) Cache is to discard the least recently used items first
I recently used git-svn to clone an SVN repository, for the purposes of maintaining
I have recently used an icon file received from our marketing team. The icon

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.