Posts

Mjuka fästingar

Image
.mw-parser-output table.ambox{margin:0 10%;border-collapse:collapse;background:#fbfbfb;border:1px solid #aaa;border-left:10px solid #608ec2}.mw-parser-output table.ambox th.ambox-text,.mw-parser-output table.ambox td.ambox-text{padding:.25em .5em;width:100%}.mw-parser-output table.ambox td.ambox-image{padding:2px 0 2px .5em;text-align:center;vertical-align:middle}.mw-parser-output table.ambox td.ambox-imageright{padding:2px 4px 2px 0;text-align:center;vertical-align:middle}.mw-parser-output table.ambox-notice{border-left:10px solid #608ec2}.mw-parser-output table.ambox-delete,.mw-parser-output table.ambox-serious{border-left:10px solid #b22222}.mw-parser-output table.ambox-content{border-left:10px solid #f28500}.mw-parser-output table.ambox-style{border-left:10px solid #f4c430}.mw-parser-output table.ambox-merge{border-left:10px solid #9932cc}.mw-parser-output table.ambox-protection{border-left:10px solid #bba}.mw-parser-output .ambox+.ambox,.mw-parser-output .topbox+.ambox,.mw-pars...

Why is 2 * x * x faster than 2 * ( x * x ) in Python 3.x, for integers?

Image
up vote 26 down vote favorite 11 The following Python 3.x integer multiplication takes on average between 1.66s and 1.77s: import time start_time = time.time() num = 0 for x in range(0, 10000000): # num += 2 * (x * x) num += 2 * x * x print("--- %s seconds ---" % (time.time() - start_time)) if I replace 2 * x * x with 2 *(x * x) , it takes between 2.04 and 2.25 . How come? On the other hand it is the opposite in Java: 2 * (x * x) is faster in Java. Java test link: Why is 2 * (i * i) faster than 2 * i * i in Java? I ran each version of the program 10 times, here are the results. 2 * x * x | 2 * (x * x) --------------------------------------- 1.7717654705047607 | 2.0789272785186768 1.735931396484375 | 2.1166207790374756 1.7093875408172607 | 2.024367570877075 1.7004504203796387...