1
0
forked from Mirror/wren
Files
wren/benchmark/method_call.py
Bob Nystrom e7cf8ffe2e Make method_call benchmark use inheritance.
This also fixes a bug where constructors weren't being bound
correctly, and eliminates some unneeded instructions when
compiling ifs.

I also tweaked the other method_call languages to match Wren's.
Since Wren needs to do a super call to get to the parent _count,
the other languages do now too.

This is nice too because it means we're benchmarking super 
calls.
2013-12-18 07:37:41 -08:00

56 lines
1.1 KiB
Python

#!/usr/bin/python
# http://www.bagley.org/~doug/shootout/
import sys
import time
class Toggle(object):
def __init__(self, start_state):
self.bool = start_state
def value(self):
return(self.bool)
def activate(self):
self.bool = not self.bool
return(self)
class NthToggle(Toggle):
def __init__(self, start_state, max_counter):
Toggle.__init__(self, start_state)
self.count_max = max_counter
self.counter = 0
def activate(self):
self.counter += 1
if (self.counter >= self.count_max):
super(NthToggle, self).activate()
self.counter = 0
return(self)
def main():
start = time.clock()
NUM = 1000000
val = 1
toggle = Toggle(val)
for i in xrange(0,NUM):
val = toggle.activate().value()
if val:
print "true"
else:
print "false"
val = 1
ntoggle = NthToggle(val, 3)
for i in xrange(0,NUM):
val = ntoggle.activate().value()
if val:
print "true"
else:
print "false"
print("elapsed: " + str(time.clock() - start))
main()