"to" vs "method"
Thomas Leonard <[email protected]>
| Newsgroups | gmane.comp.lang.e.general |
|---|---|
| Message-ID | <[email protected]> |
I just discovered that there are two ways of defining a method in E:
def foo1 {
to run() { return 3 }
}
def foo2 {
method run() { 3 }
}
Interestingly, calling foo2() is between 3 and 10 times faster than
calling foo1(), depending on how deeply nested the call is:
def time(cb) {
def a := timer.now()
for x in 1..100000 {
cb()
}
def b := timer.now()
return (b - a) / 100000
}
# Warm up
time(fn { foo1() })
time(fn { foo2() })
def nested(level, cb) {
if (level == 0) {
return cb()
} else {
return nested(level - 1, cb)
}
}
for depth in [0, 10, 100, 1000] {
def a := nested(depth, fn { time( fn { foo1() } ) })
def b := nested(depth, fn { time( fn { foo2() } ) })
println(`At depth $depth:
foo1 takes $a ms and foo2 takes $b ms`)
}
Prints:
At depth 0:
foo1 takes 0.02525 ms and foo2 takes 0.00833 ms
At depth 10:
foo1 takes 0.03042 ms and foo2 takes 0.00848 ms
At depth 100:
foo1 takes 0.08003 ms and foo2 takes 0.00845 ms
At depth 1000:
foo1 takes 0.08508 ms and foo2 takes 0.00859 ms
I guess this is because "to" uses __return, which is implemented by
throwing an exception in Java (so speed depends on the size of the
stack).
--
Dr Thomas Leonard ROX desktop / Zero Install
GPG: 9242 9807 C985 3C07 44A6 8B9A AE07 8280 59A5 3CC1
GPG: DA98 25AE CAD0 8975 7CDA BD8E 0713 3F96 CA74 D8BA