Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #8038 > unrolled thread
| Started by | Rouslan Korneychuk <rouslank@msn.com> |
|---|---|
| First post | 2011-06-20 19:47 -0400 |
| Last post | 2011-06-21 16:06 -0400 |
| Articles | 3 — 2 participants |
Back to article view | Back to comp.lang.python
basic bytecode to machine code compiler (part 3) Rouslan Korneychuk <rouslank@msn.com> - 2011-06-20 19:47 -0400
Re: basic bytecode to machine code compiler (part 3) Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> - 2011-06-21 12:55 +0200
Re: basic bytecode to machine code compiler (part 3) Rouslan Korneychuk <rouslank@msn.com> - 2011-06-21 16:06 -0400
| From | Rouslan Korneychuk <rouslank@msn.com> |
|---|---|
| Date | 2011-06-20 19:47 -0400 |
| Subject | basic bytecode to machine code compiler (part 3) |
| Message-ID | <1_QLp.10563$xh5.2244@newsfe02.iad> |
My compiler now supports the x86-64 instruction set, in addition to x86.
It also generates faster x86 machine code.
Although it's designed to support 64-bit Windows, I have only tested it
on Linux so far, and it doesn't support running with Windows' DEP yet.
It's available at https://github.com/Rouslan/nativecompile
Here are the new benchmark results. The 64-bit code has a better margin
of improvement even though the Python interpreter is already faster in
64-bit mode.
The first test performs a quicksort on a list of 100 numbers, 5000
times. The second calculates all the prime numbers up to 10000000. Each
test is run three times in a row, first with the interpreter, then with
the compiled code.
#### SCRIPT ONE ####
import time
import random
import nativecompile
bcode = compile('''
def quicksort(array):
if len(array) <= 1:
return array
pindex = len(array)//2
pivot = array[pindex]
less = []
greater = []
for i,x in enumerate(array):
if i != pindex:
(less if x <= pivot else greater).append(x)
return quicksort(less) + [pivot] + quicksort(greater)
in_ = list(range(100))
random.seed(346097)
random.shuffle(in_)
t = time.clock()
for x in range(5000):
out = quicksort(in_)
t = time.clock()-t
assert out == sorted(in_)
print('execution time: {}'.format(round(t,10)))
''','<string>','exec')
mcode = nativecompile.compile(bcode)
print('byte code')
for x in range(3): eval(bcode)
print()
print('machine code')
for x in range(3): mcode()
print()
#### X86 OUTPUT ####
byte code
execution time: 1.76
execution time: 1.75
execution time: 1.77
machine code
execution time: 1.43
execution time: 1.43
execution time: 1.43
#### X86-64 OUTPUT ####
byte code
execution time: 1.49
execution time: 1.48
execution time: 1.48
machine code
execution time: 1.17
execution time: 1.17
execution time: 1.17
#### SCRIPT TWO ####
import time
import math
import nativecompile
bcode = compile('''
def primes_list(upto):
nums = [True] * (upto//2-1)
for i in range(3,math.floor(math.sqrt(upto))+1,2):
if nums[i//2-1]:
for j in range(i*3,upto,i*2):
nums[j//2-1] = False
primes = []
for i,n in enumerate(nums):
if n: primes.append((i+1)*2+1)
return primes
t = time.clock()
primes = primes_list(10000000)
t = time.clock()-t
print(primes[-1])
print('execution time: {}'.format(round(t,10)))
''','<string>','exec')
mcode = nativecompile.compile(bcode)
print('byte code')
for x in range(3): eval(bcode)
print()
print('machine code')
for x in range(3): mcode()
print()
#### X86 OUTPUT ####
byte code
9999991
execution time: 3.57
9999991
execution time: 3.34
9999991
execution time: 3.37
machine code
9999991
execution time: 2.73
9999991
execution time: 2.67
9999991
execution time: 2.73
#### X86-64 OUTPUT ####
byte code
9999991
execution time: 3.05
9999991
execution time: 3.24
9999991
execution time: 3.17
machine code
9999991
execution time: 2.15
9999991
execution time: 2.17
9999991
execution time: 2.17
[toc] | [next] | [standalone]
| From | Ulrich Eckhardt <ulrich.eckhardt@dominolaser.com> |
|---|---|
| Date | 2011-06-21 12:55 +0200 |
| Message-ID | <ol96d8-hhd.ln1@satorlaser.homedns.org> |
| In reply to | #8038 |
Rouslan Korneychuk wrote: > if i != pindex: > (less if x <= pivot else greater).append(x) Just curious, is there a reason why you wrote this last line that way instead of using a "normal" if/else clause? Cheers! Uli -- Domino Laser GmbH Geschäftsführer: Thorsten Föcking, Amtsgericht Hamburg HR B62 932
[toc] | [prev] | [next] | [standalone]
| From | Rouslan Korneychuk <rouslank@msn.com> |
|---|---|
| Date | 2011-06-21 16:06 -0400 |
| Message-ID | <cR6Mp.10110$PA5.6044@newsfe01.iad> |
| In reply to | #8083 |
On 06/21/2011 06:55 AM, Ulrich Eckhardt wrote: > Rouslan Korneychuk wrote: >> if i != pindex: >> (less if x<= pivot else greater).append(x) > > Just curious, is there a reason why you wrote this last line that way > instead of using a "normal" if/else clause? > > > Cheers! > > Uli > > No special reason. I just like concise code.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web