Why is my exploded Python code faster?

I am participating in the Comp-sci introductory class (after multi-discipline web programming), and I was curious how fast I gained speed, if any, with my one-liner languages.

for line in lines:
  numbers.append(eval(line.strip().split()[0]))

So, I wrote the same thing with painfully explicit assignments and ran them against each other.

for line in lines:
  a = line.split()
  b = a[0]
  c = b.strip()
  d = eval(c)
  numbers.append(d)

The second one works on an ongoing basis for 30 ms (in my FreeBSD shell account, see Edit No. 2) with an input file of 100 thousand lines! Of course, this is a total runtime of 3 seconds, so the percentage is small ... but I am very surprised to see how all these explicit assignments are assigned in some way.

There's a recent thread about feature performance, not inline code, but that seems even more basic. What gives? Should I write lovingly verbose code and tell my gloomy colleagues for performance reasons? (Fortunately, the version for compiling the list works at a speed of about 10 ms, so my cherished compactness does not completely exit the window.)

EDIT: Thanks for the tip about my messy code extension. You are all right that the second should really be:

for line in lines:
  a = line.strip()
  b = a.split()
  c = b[0]
  d = eval(c)
  numbers.append(d)

However, even after I fixed this, my timings are 2.714s, 2.652s and 2.624s respectively, for a single line, fully exploded and list comprehension (not shown). Therefore, my question is!

№ 2: , , ! , , , , , . , , , ", , - ". , , - Debian Windows. , !

+3
8

, , .

Python - :

, , : Python , , ,

, . :

, , :

>>> def a(lines):
    for line in lines:
        numbers.append(eval(line.strip().split()[0]))

>>> def b(lines):
    for line in lines:
        a = line.strip()
        b = a.split()
        c = b[0]
        d = eval(c)
        numbers.append(d)

:

>>> import dis
>>> dis.dis(a)
  2           0 SETUP_LOOP              49 (to 52)
              3 LOAD_FAST                0 (lines)
              6 GET_ITER            
        >>    7 FOR_ITER                41 (to 51)
             10 STORE_FAST               1 (line)

  3          13 LOAD_GLOBAL              0 (numbers)
             16 LOAD_ATTR                1 (append)
             19 LOAD_GLOBAL              2 (eval)
             22 LOAD_FAST                1 (line)
             25 LOAD_ATTR                3 (strip)
             28 CALL_FUNCTION            0
             31 LOAD_ATTR                4 (split)
             34 CALL_FUNCTION            0
             37 LOAD_CONST               1 (0)
             40 BINARY_SUBSCR       
             41 CALL_FUNCTION            1
             44 CALL_FUNCTION            1
             47 POP_TOP             
             48 JUMP_ABSOLUTE            7
        >>   51 POP_BLOCK           
        >>   52 LOAD_CONST               0 (None)
             55 RETURN_VALUE        
>>> dis.dis(b)
  2           0 SETUP_LOOP              73 (to 76)
              3 LOAD_FAST                0 (lines)
              6 GET_ITER            
        >>    7 FOR_ITER                65 (to 75)
             10 STORE_FAST               1 (line)

  3          13 LOAD_FAST                1 (line)
             16 LOAD_ATTR                0 (strip)
             19 CALL_FUNCTION            0
             22 STORE_FAST               2 (a)

  4          25 LOAD_FAST                2 (a)
             28 LOAD_ATTR                1 (split)
             31 CALL_FUNCTION            0
             34 STORE_FAST               3 (b)

  5          37 LOAD_FAST                3 (b)
             40 LOAD_CONST               1 (0)
             43 BINARY_SUBSCR       
             44 STORE_FAST               4 (c)

  6          47 LOAD_GLOBAL              2 (eval)
             50 LOAD_FAST                4 (c)
             53 CALL_FUNCTION            1
             56 STORE_FAST               5 (d)

  7          59 LOAD_GLOBAL              3 (numbers)
             62 LOAD_ATTR                4 (append)
             65 LOAD_FAST                5 (d)
             68 CALL_FUNCTION            1
             71 POP_TOP             
             72 JUMP_ABSOLUTE            7
        >>   75 POP_BLOCK           
        >>   76 LOAD_CONST               0 (None)
             79 RETURN_VALUE        

, , STORE_FAST, LOAD_FAST - . , () , .

+3

. :

A > B > C > D > E 

B > C > A > D > E

, strip() 2 , , .

+15

, , , - , . (- ), .

- - Python .

: () .
, , , ( , ).
, , , .
() dis (python disassembler) , , . , .

, , eval ​​ . , .
:

eval_ = eval
for line in lines:
    a = line.strip()
    b = a.split()
    c = b[0]
    d = eval_(c)
    numbers.append(d)

-, - , .

+7

:

for line in lines:
    numbers.append(eval(line.strip().split()[0]))

:

for line in lines:
    numbers.append(eval(line.split()[0].strip()))
+4

; ; , , , .

, : . line.strip().split() , ; , strip() . strip() ; , , split(), . , strip() .

, , strip() , . strip() ; , . . , , . , strip() , , , .

, , , . :

for line in lines:
  numbers.append(eval(line.split()[0].strip()))

, strip(). . , , , strip(), , , , , .

, split "maxsplit"; , . , split(None, 1). , strip(). :

for line in lines:
  numbers.append(eval(line.split(None, 1)[0]))

, , int() eval(), .

+3

, . ? , Python, ? , ?

+3

. , eval() .

eval?

+2

, . ( 0 4 , 0) . , ( , , ).

? ( ):

one-line: 13.208s
expanded: 26.321s
listcomp: 13.024s

Ubuntu 9.04, 32-, Python 2.6.2 (CPython, ).

, , , , , .

Python, :

import random

f = open("/tmp/junk.txt", "w")

r = random.Random()

def randws():
    n = r.randint(0, 10) - 4
    if n < 0 or n > 4:
        n = 0
    return " " * n

for i in xrange(1000000):
    s0 = randws()
    n = r.randint(0, 256)
    s1 = randws()
    f.write("%s%d%s\n" % (s0, n, s1))

:

lines = open("/tmp/junk.txt")

numbers = [eval(line.split(None, 1)[0]) for line in lines]

P.S. , , int float.

lines = open("/tmp/junk.txt")

def val(x):
    try:
        return int(x)
    except ValueError:
        pass

    try:
        return float(x)
    except StandardError:
        return 0

numbers = [val(line.split(None, 1)[0]) for line in lines]

His best three times was: 2.161s

+2
source

Source: https://habr.com/ru/post/1716722/


All Articles