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

  • Home
  • SEARCH
  • 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 3355498
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:24:10+00:00 2026-05-18T02:24:10+00:00

See Is there something wrong with this python code, why does it run so

  • 0

See Is there something wrong with this python code, why does it run so slow compared to ruby? for my previous attempt at understanding the differences between python and ruby.

As pointed out by igouy the reasoning I came up with for python being slower could be something else than due to recursive function calls (stack involved).

I made this

#!/usr/bin/python2.7
i = 0
a = 0
while i < 6553500:
    i += 1
    if i != 6553500:
       a = i 
    else:
        print "o" 
print a 

In ruby it is

#!/usr/bin/ruby
i = 0
a = 0
while i < 6553500
    i += 1
    if i != 6553500
       a = i 
    else
        print "o"
    end
end   
print a

Python 3.1.2 (r312:79147, Oct 4 2010, 12:45:09)
[GCC 4.5.1] on linux2

time python pytest.py
o

6553499

real 0m3.637s

user 0m3.586s

ruby 1.9.2p0 (2010-08-18 revision 29036) [x86_64-linux]
time ruby rutest.rb

o6553499

real 0m0.618s

user 0m0.610s

Letting it loop higher gives higher differences. Adding an extra 0, ruby finishes in 7s, while python runs for 40s.

This is run on Intel(R) Core(TM) i7 CPU M 620 @ 2.67GHz with 4GB mem.

Why is this so?

  • 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-05-18T02:24:10+00:00Added an answer on May 18, 2026 at 2:24 am

    First off, note that the Python version you show is incorrect: you’re running this code in Python 2.7, not 3.1 (it’s not even valid Python3 code). (FYI, Python 3 is usually slower than 2.)

    That said, there’s a critical problem in the Python test: you’re writing it as global code. You need to write it as a function. It runs about twice as fast when written correctly, in both Python 2 and 3:

    def main():
        i = 0
        a = 0
        while i < 6553500:
            i += 1
            if i != 6553500:
               a = i
            else:
                print("o")
        print(a)
    
    if __name__ == "__main__":
        main()
    

    When you write code globally, you have no locals; all of your variables are global variables. Locals are much faster than globals in Python, because globals are stored in a dict. Locals can be referenced directly by the VM by index, so no hash table lookups are needed.

    Also, note that this is such a simple test, what you’re really doing is benchmarking a few arbitrary bytecode operations.

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

Sidebar

Related Questions

I thought there was something in GExperts to do this, but I can't see
Shouldn't this be a pretty straightforward operation? However, I see there's neither a size()
I see there are a few . Which ones are maintained and easy to
What is the best method for user authorisation/authentication in ASP.NET MVC? I see there
I see that there is Controller.RedirectToAction(string actionName, string controllerName, object values) but I never
I'm trying to see if there's a way to get a consistent unit test
I am reviewing a big java application to see if there are any performance
I'm working with jQuery and looking to see if there is an easy way
Is there a good way to see what format an image is, without having
I see that within MySQL there are Cast() and Convert() functions to create integers

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.