|
Loki
Marc Fauconneau has released an initial version of his Loki addon for Io. Loki is basically allows Io to generate an execute x86 machine code at runtime. Marc has written a C-level like sublanguage within Io that can be be used to compile highly optimized code where needed. Here is an example of the nseive algorithm from The Computer Language Shootout implemented in Loki. It runs only 20% slower than the equivalent C code compiled with gcc.
Loki Implementation clone linkToAs( Sequence, "nsieve" ) with(
bytes := _self data.ptr castAs(ByteArray) bytes
n := valueArgAt(0) data.d
cnt := int <- 0
Loop as(i := int <- 2) while(i<n) by(i++) with(
(bytes[i] == 0) ifTrue(
Loop for(k := i+i; k<n; k += i) with(
bytes[k] <- 1
)
cnt++
)
)
_self state numberWithDouble(cnt asFloat64)
)
nsieve := method(n,
primes := Sequence clone setSize(n+1) zero
cnt := primes nsieve(n)
( "Primes up to" .. n asString alignRight(9, " ") .. cnt asString alignRight(9, " ") ) println
)
nsieve( (2**(n))*10000 )
nsieve( (2**(n-1))*10000 )
nsieve( (2**(n-2))*10000 )
|