niklas-heer / speed-comparison

A repo which compares the speed of different programming languages.
https://niklas-heer.github.io/speed-comparison
MIT License
508 stars 79 forks source link

Here is a BooBoo version #120

Open ghost opened 1 year ago

ghost commented 1 year ago
number rounds
= rounds 100000000
* rounds 2
+ rounds 3

number x
= x 1
number pi
= pi 1.0
number a
number b

number i
= i 3
:next_loop
neg x
= a 1
= b i
/ a b
* a x
+ pi a
+ i 2
? i rounds
jl next_loop

* pi 4

print "%\n" pi
ghost commented 1 year ago

Here is a BooBoo version with some improvements that reads the number of rounds from a file.

number f
file_open f "rounds.txt" "r"
string s
file_read f s
file_close f
number rounds
= rounds (+ 3 (* s 2))

number x
= x 1
number pi
= pi 1

number i
= i 3
:next_loop
neg x
+ pi (/ x i)
+ i 2
? i rounds
jl next_loop

* pi 4

print "%\n" pi

EDIT: updated it to make it a little bit faster. Perhaps the other benchmarks would benefit.

The only issue now is text formatting, it simply uses %g so it doesn't print all the digits it calculated.

ghost commented 1 year ago

I've added formatting options so it's fully functional.

number f
file_open f "rounds.txt" "r"
string s
file_read f s
file_close f
number rounds
= rounds (+ 3 (* s 2))

number x
= x 1
number pi
= pi 1

number i
= i 3
:next_loop
neg x
+ pi (/ x i)
+ i 2
? i rounds
jl next_loop

* pi 4

print "%(3.16g)\n" pi
ghost commented 1 year ago

It's a bit slower than Python but faster than ruby or perl for me.

ghost commented 1 year ago

I added for loops to BooBoo and that cut the overhead significantly, it is now quite a bit faster than Python but still slower than Lua.

number f
file_open f "rounds.txt" "r"
string s
file_read f s
file_close f
number rounds
= rounds (+ 3 (* s 2))

number x
= x 1
number pi
= pi 1
number i
= i 3

for i rounds 2 loop_end
neg x
+ pi (/ x i)
:loop_end

* pi 4

print "%(3.16g)\n" pi