Quote (Minkomonster @ Jan 15 2015 07:08pm)
It's not even optimization though, when it compiles it will look as it does now. It's just ugly code.
hmm i was just under the impression interpreted languages aren't very well optimized, but im not familiar with the internal working of python. i ran it through "dis" to dissemble the bytecodes and it still pops up
https://docs.python.org/2/library/dis.htmlCode
>>> def quadform(a, b, c):
... d = b**2-4*a*c # discriminant
... if d < 0:
... print ("This equation has no real solution")
... elif d == 0:
... x = (-b+math.sqrt(b**2-4*a*c))/2*a
... print ("This equation has one solutions: "), x
... else:
... x1 = (-b+math.sqrt((b**2)-(4*(a*c))))/(2*a)
... x2 = (-b-math.sqrt((b**2)-(4*(a*c))))/(2*a)
... print ("This equation has two solutions: ", x1, " or", x2)
...
>>> dis.dis(quadform)
2 0 LOAD_FAST 1 (b)
3 LOAD_CONST 1 (2)
6 BINARY_POWER
7 LOAD_CONST 2 (4)
10 LOAD_FAST 0 (a)
13 BINARY_MULTIPLY
14 LOAD_FAST 2 (c)
17 BINARY_MULTIPLY
18 BINARY_SUBTRACT
19 STORE_FAST 3 (d)
3 22 LOAD_FAST 3 (d)
25 LOAD_CONST 3 (0)
28 COMPARE_OP 0 (<)
31 POP_JUMP_IF_FALSE 42
4 34 LOAD_CONST 4 ('This equation has no real solution')
37 PRINT_ITEM
38 PRINT_NEWLINE
39 JUMP_FORWARD 173 (to 215)
5 >> 42 LOAD_FAST 3 (d)
45 LOAD_CONST 3 (0)
48 COMPARE_OP 2 (==)
51 POP_JUMP_IF_FALSE 110
6 54 LOAD_FAST 1 (b)
57 UNARY_NEGATIVE
58 LOAD_GLOBAL 0 (math)
61 LOAD_ATTR 1 (sqrt)
64 LOAD_FAST 1 (b)
67 LOAD_CONST 1 (2)
70 BINARY_POWER
71 LOAD_CONST 2 (4)
74 LOAD_FAST 0 (a)
77 BINARY_MULTIPLY
78 LOAD_FAST 2 (c)
81 BINARY_MULTIPLY
82 BINARY_SUBTRACT
83 CALL_FUNCTION 1
86 BINARY_ADD
87 LOAD_CONST 1 (2)
90 BINARY_DIVIDE
91 LOAD_FAST 0 (a)
94 BINARY_MULTIPLY
95 STORE_FAST 4 (x)
7 98 LOAD_CONST 5 ('This equation has one solutions: ')
101 PRINT_ITEM
102 LOAD_FAST 4 (x)
105 PRINT_ITEM
106 PRINT_NEWLINE
107 JUMP_FORWARD 105 (to 215)
9 >> 110 LOAD_FAST 1 (b)
113 UNARY_NEGATIVE
114 LOAD_GLOBAL 0 (math)
117 LOAD_ATTR 1 (sqrt)
120 LOAD_FAST 1 (b)
123 LOAD_CONST 1 (2)
126 BINARY_POWER
127 LOAD_CONST 2 (4)
130 LOAD_FAST 0 (a)
133 LOAD_FAST 2 (c)
136 BINARY_MULTIPLY
137 BINARY_MULTIPLY
138 BINARY_SUBTRACT
139 CALL_FUNCTION 1
142 BINARY_ADD
143 LOAD_CONST 1 (2)
146 LOAD_FAST 0 (a)
149 BINARY_MULTIPLY
150 BINARY_DIVIDE
151 STORE_FAST 5 (x1)
10 154 LOAD_FAST 1 (b)
157 UNARY_NEGATIVE
158 LOAD_GLOBAL 0 (math)
161 LOAD_ATTR 1 (sqrt)
164 LOAD_FAST 1 (b)
167 LOAD_CONST 1 (2)
170 BINARY_POWER
171 LOAD_CONST 2 (4)
174 LOAD_FAST 0 (a)
177 LOAD_FAST 2 (c)
180 BINARY_MULTIPLY
181 BINARY_MULTIPLY
182 BINARY_SUBTRACT
183 CALL_FUNCTION 1
186 BINARY_SUBTRACT
187 LOAD_CONST 1 (2)
190 LOAD_FAST 0 (a)
193 BINARY_MULTIPLY
194 BINARY_DIVIDE
195 STORE_FAST 6 (x2)
11 198 LOAD_CONST 6 ('This equation has two solutions: ')
201 LOAD_FAST 5 (x1)
204 LOAD_CONST 7 (' or')
207 LOAD_FAST 6 (x2)
210 BUILD_TUPLE 4
213 PRINT_ITEM
214 PRINT_NEWLINE
>> 215 LOAD_CONST 0 (None)
218 RETURN_VALUE
>>>
not that i really know what it does anyway, heh. i remember reading the generated MSIL for a little while when i was reading one of the .NET books, but i lost interest within a few days.
This post was edited by carteblanche on Jan 15 2015 06:37pm