Do comments slow down an interpreted language?

后端 未结 11 2221
深忆病人
深忆病人 2020-12-01 09:59

I am asking this because I use Python, but it could apply to other interpreted languages as well (Ruby, PHP, JavaScript).

Am I slowing down the interpreter whenever

11条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-01 10:39

    Did up a script like Rich's with some comments (only about 500kb text):

    # -*- coding: iso-8859-15 -*-
    import timeit
    
    no_comments = """
    a = 30
    b = 40
    for i in range(10):
        c = a**i * b**i
    """
    yes_comment = """
    a = 30
    b = 40
    
    # full HTML from http://en.wikipedia.org/
    # wiki/Line_of_succession_to_the_British_throne
    
    for i in range(10):
        c = a**i * b**i
    """
    loopcomment = """
    a = 30
    b = 40
    
    for i in range(10):
        # full HTML from http://en.wikipedia.org/
        # wiki/Line_of_succession_to_the_British_throne
    
        c = a**i * b**i
    """
    
    t_n = timeit.Timer(stmt=no_comments)
    t_y = timeit.Timer(stmt=yes_comment)
    t_l = timeit.Timer(stmt=loopcomment)
    
    print "Uncommented block takes %.2f usec/pass" % (
        1e6 * t_n.timeit(number=100000)/1e5)
    print "Commented block takes %.2f usec/pass" % (
        1e6 * t_y.timeit(number=100000)/1e5)
    print "Commented block (in loop) takes %.2f usec/pass" % (
        1e6 * t_l.timeit(number=100000)/1e5)
    


    C:\Scripts>timecomment.py
    Uncommented block takes 15.44 usec/pass
    Commented block takes 15.38 usec/pass
    Commented block (in loop) takes 15.57 usec/pass
    
    C:\Scripts>timecomment.py
    Uncommented block takes 15.10 usec/pass
    Commented block takes 14.99 usec/pass
    Commented block (in loop) takes 14.95 usec/pass
    
    C:\Scripts>timecomment.py
    Uncommented block takes 15.52 usec/pass
    Commented block takes 15.42 usec/pass
    Commented block (in loop) takes 15.45 usec/pass
    

    Edit as per David's comment:

     -*- coding: iso-8859-15 -*-
    import timeit
    
    init = "a = 30\nb = 40\n"
    for_ = "for i in range(10):"
    loop = "%sc = a**%s * b**%s"
    historylesson = """
    #  
    """
    tabhistorylesson = """
        #  
    """
    
    s_looped = init + "\n" + for_ + "\n" + tabhistorylesson + loop % ('   ','i','i')
    s_unroll = init + "\n"
    for i in range(10):
        s_unroll += historylesson + "\n" + loop % ('',i,i) + "\n"
    t_looped = timeit.Timer(stmt=s_looped)
    t_unroll = timeit.Timer(stmt=s_unroll)
    
    print "Looped length: %i, unrolled: %i." % (len(s_looped), len(s_unroll))
    
    print "For block takes %.2f usec/pass" % (
        1e6 * t_looped.timeit(number=100000)/1e5)
    print "Unrolled it takes %.2f usec/pass" % (
        1e6 * t_unroll.timeit(number=100000)/1e5)
    


    C:\Scripts>timecomment_unroll.py
    Looped length: 623604, unrolled: 5881926.
    For block takes 15.12 usec/pass
    Unrolled it takes 14.21 usec/pass
    
    C:\Scripts>timecomment_unroll.py
    Looped length: 623604, unrolled: 5881926.
    For block takes 15.43 usec/pass
    Unrolled it takes 14.63 usec/pass
    
    C:\Scripts>timecomment_unroll.py
    Looped length: 623604, unrolled: 5881926.
    For block takes 15.10 usec/pass
    Unrolled it takes 14.22 usec/pass
    

提交回复
热议问题