Python

math

if

import time
print(time.time())
*# Version 1: compute absolute value with abs.*
a = -1
i = 0
**while** i < 10000000:
b = abs(a)
i += 1
print(time.time())
*# Version 2: compute absolute value with if-statement.*
a = -1
i = 0
**while** i < 10000000:
if a < 0:
b = -a
else:
b = a
i += 1
print(time.time())1346355970.511
1346355973.081 (Abs = *2.57 s*)
1346355975.509 (If = *2.428 s*)

Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.

Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.

This page was last updated on May 23, 2023 (edit).