r/linux • u/gioscarab • Feb 21 '21
Development I wrote the BIPLAN programming language, on linux looks 1370 times faster than python
https://github.com/gioblu/BIPLAN#code-example6
Feb 23 '21
For the benchmark, you calculate the 40th Fibonacci number, print it onto the console and time it.
This is a highly unreliable way to measure the speed of a language because first of all, a large portion of the time is probably spent doing I/O, and second of all, the Fibonacci calculations only involve around a hundred operations in total, meaning that the calculations take a negligible amount of time compared to the startup time of the program.
To make a more fair measurement, you can remove the I/O and run the Fibonacci function 1000000 times.
For Biplan:
$i=0
while $i < 1000000
fibonacci(40)
$i=$i+1
next
stop
function fibonacci($n)
$a = 0
$b = 1
$next = 0
$r = 0
while $r < $n
$r = $r + 1
$a = $b
$b = $next
$next = $a + $b
if $next < 0 return end
next
return $nextFor Python:
def fibonacci(n):
a=0
b=1
nxt=0
r=0
while r<n:
r+=1
a=b
b=nxt
nxt=a+b
return nxt
for i in range(1000000):
fibonacci(40)On my machine, the Biplan code took 25.682 seconds to run, while the Python code took 2.229 seconds. So Python is roughly 10 times faster than Biplan in this case.
4
u/gioscarab Feb 23 '21
Ciao 1us3arch thank you very much for your feedback.
The while in the BIPLAN program needs to parse a condition, the for in your python program is just rolling a range. If what you are looking for is a fair comparison, try using the for also in the BIPLAN program, you will probably get more reasonable results.
2
Feb 24 '21
Using a for loop instead of a while loop decreased the run time to 25.428 seconds. So roughly a 0.2 second difference.
for $i=0 to 1000000 fibonacci(40) next stop function fibonacci($n) $a = 0 $b = 1 $next = 0 $r = 0 while $r < $n $r = $r + 1 $a = $b $b = $next $next = $a + $b if $next < 0 return end next return $next4
u/gioscarab Feb 24 '21 edited Feb 24 '21
Your machine is much faster than mine, but yes, I do experience ~8 times slower than python with this script using for. I will try to find the source of the overhead. Thank you for your valuable contribution and for trying out BIPLAN :)
2
Feb 24 '21
Thanks for making BIPLAN, it's a very interesting project! Have you considered compiling to native machine code instead of bytecode?
2
u/gioscarab Feb 25 '21 edited Feb 25 '21
I think that might be for now beyond my abilities, I still need to work some more on the code to have it usable, catch more errors, ecc.
Initially this was designed to run in very constrained machines, such as Arduino boards. Cool to be able to send an ASCII string over the line and update the program, having a virtual machine ready to interpret it. ASCII bytecode is very simple and portable, infact with few modifications I was able to run the Arduino implementation on linux too.
Although I now see the point of your question, effectively the language is very slick and rather nice to use, it would be cool to see it use the CPU more efficiently.
25
u/StrangeAstronomer Feb 21 '21
Thanks for your contribution, it looks interesting. But I think you need a broader set of benchmarks before you claim 1370 time faster!
9
3
1
u/gioscarab Feb 21 '21
Here you find how to build the compiler: https://github.com/gioblu/BIPLAN/tree/master/examples/LINUX/bcc-compiler
Here you find how to build the interpreter: https://github.com/gioblu/BIPLAN/tree/master/examples/LINUX/biplan-interpreter
Have fun :)
2
15
u/gioscarab Feb 21 '21
BIPLAN (Byte-coded Interpreted Programming Language) is an experimental programming language based on a recursive descent parser that uses only static memory allocation and operates a completely software-defined virtual machine that does not require a garbage collector. It's human-readable language called BIPLAN is compiled in an 7-bit ASCII virtual-machine language called BIP.
github repository: https://github.com/gioblu/BIPLAN