Can Go really be that much faster than Python?

前端 未结 8 1430
抹茶落季
抹茶落季 2021-01-31 14:27

I think I may have implemented this incorrectly because the results do not make sense. I have a Go program that counts to 1000000000:

package main

import (
             


        
8条回答
  •  执念已碎
    2021-01-31 14:59

    pypy actually does an impressive job of speeding up this loop

    def main():
        x = 0
        while x < 1000000000:
            x+=1
    
    if __name__ == "__main__":
        s=time.time()
        main()
        print time.time() - s
    

    $ python count.py 
    44.221405983
    $ pypy count.py 
    1.03511095047
    

    ~97% speedup!

    Clarification for 3 people who didn't "get it". The Python language itself isn't slow. The CPython implementation is a relatively straight forward way of running the code. Pypy is another implementation of the language that does many tricky (especiallt the JIT) things that can make enormous differences. Directly answering the question in the title - Go isn't "that much" faster than Python, Go is that much faster than CPython.

    Having said that, the code samples aren't really doing the same thing. Python needs to instantiate 1000000000 of its int objects. Go is just incrementing one memory location.

提交回复
热议问题