I'm from China, a newbie here, have programmed in C/C++/Golang over 3 years(others are skin-deep). Without being satisfied with any of them like our author, I'm seeking a language which applies from bare metal to frontend and Nim give me a thrill. Given most my languages are system programming ones, javascript makes no sense to me, single thread, interpreter like Python which can't take advantage of modern CPU-the reason why I convinced CTO to rewrite core services in Golang rather than Python. I also found dom.nim wrapper in the Nim/lib/js directory, so is it a chance to 'rewrite' javascript in Nim, or what we can do is just implement virtual DOM in Nim like ReactJS in js? There is a proverb in China, That which is long divided must unify; that which is long unified must divide. I think the control of underlying hardware,system programming,web,app are going to unify,probably in Nim. Furthermore, China will become the largest high-tech market in the world during the next two decades even Obama Pledges $4 Billion to Computer Science in US Schools. I believe everyone here who knows Nim, willing to get involved in, sees the spark of Nim as others do or not, has already taken a step closer to the future. I'd love to get in touch with you and share what I know and see to make the world better.
import os
var
timer,main :Thread[void]
var chan:Channel[bool]
proc threadFunc() {.thread.} =
sleep(1)
chan.send(true)
proc threadMain(){.thread.}=
var counter:int
var dest ="apple"
while true:
if chan.recv():
echo "break"
break
while(dest.len<100):
var
offs=dest.len-5
i=offs
while i<offs+10:
dest.add(dest[i])
i.inc
counter.inc
echo counter
open(chan)
createThread(main, threadMain)
createThread(timer, threadFunc)
joinThread(timer)
joinThread(main)
close(chan)
this is a Nim equivalent of the offical javascript string performance benchmark, which meant to count the number of iterations in 1 second but resulted in breaking immediately.To make the OP code run use this:
import os
var
timer,main :Thread[void]
var chan:Channel[bool]
proc threadFunc() {.thread.} =
sleep(1000)
chan.send(true)
proc threadMain(){.thread.}=
var counter:int
#var dest ="apple"
while true:
let (brk, _) = chan.tryRecv() # notice: ignores msg!
if brk:
echo "break"
break
var dest ="apple" # guess that needs to be here
while(dest.len<100):
var
offs=dest.len-5
i=offs
while i<offs+10:
dest.add(dest[i])
i.inc
counter.inc
echo counter
open(chan)
createThread(main, threadMain)
createThread(timer, threadFunc)
joinThread(timer)
joinThread(main)
close(chan)
Edit: I changed the code so that it actually does the test as I think it was planned :)
Above code roughly gives 1,7 million iters/s
Using nimbench module:
import nimbench
bench(str, m):
for n in 0..<m:
var dest ="apple"
while(dest.len<100):
var
offs=dest.len-5
i=offs
while i<offs+10:
dest.add(dest[i])
i.inc
doNotOptimizeAway(dest)
runBenchmarks()
This gives about 2,42 million iters/s (when not querying the channel). I believe this difference already shows why using the "sleep / break" method is not a good way to measure this!
Oh, I see now. csources is a separate repo. I just needed to update that before running bin/nim c koch. I'll edit my post.
I still cannot call the program bench.nim. Doesn't that seem unreasonable?
Bench uses "nimbench". Which is to be installed as nimble package (nimble install nimbench).
Maybe you want to check out "nim-vm" (github) which makes it easy to install multiple nim and keep the current devel version up to date.
with the -d:release option I got this benchmark result
============================================================================
GlobalBenchmark relative time/iter iters/s
============================================================================
GlobalBenchmark 385.59ps 2.59G
============================================================================
test.nim relative time/iter iters/s
============================================================================
str 499.60ns 2.00M
====================================================================
sleep/break benchmark module
======================================================================
@OderWat 17M 242M
======================================================================
my 1.5M 2.00M
======================================================================
I guess your computer is better than mine.
any result is better than the origin javascript benchmark
http://jsperf.com/string-vs-array-concat/2
var dest = 'apple';
while (dest.length < 100) {
var offs = dest.length - 5;
for (var i = offs; i < offs + 10; i++) {
dest += dest.charAt(i);
}
}
which results in 67,738(67K) Ops/sec.
So is it fare enough to 'rewrite' javascript in Nim since our author has already prepared dom.nim wrapper?
We implement DOM of Nim, provide a Nim browser which parses DOM and renders GUI, then javascript can step off the history.:)
bench.nim(11, 7) Error: undeclared identifier: 'str'
But if I rename it -- e.g. bench0.nim or foo.nim -- then it works. I can have multiple copies, all with crazy names. It fails only if I use names which are already used somewhere, like bench.nim, strfmt.nim, and system.nim. It seem odd that the filename is so important.