start := time.Now().Nanosecond()
operation()
end := time.Now().Nanosecond()
fmt.Println(end - start)
package main
import (
"fmt"
"time"
)
func fib(n int) uint64 {
if n < 2 {
return 1
}
return (fib(n-1) + fib(n-2))
}
func main() {
var n = 44
for i := 0; i < 10; i++ {
start := time.Now().Nanosecond()
fib(n)
end := time.Now().Nanosecond()
fmt.Println(end - start)
}
}
I want to measure the time some operation takes. So I basically do this:start := time.Now().Nanosecond()
operation()
end := time.Now().Nanosecond()
fmt.Println(end - start)
Show us the code, perhaps we can make it faster.
Basically, he's benchmarking function calls in Go vs. Java. I would guess that the difference is due to Go's split stacks.
Yeah after I read the code I was thinking that go doesn't yet do
http://en.wikipedia.org/wiki/Tail_recursion
Not sure about that though.
--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
It would probably help to benchmark something more representative of the program you would like to write. This particular benchmark is measuring a cost that almost never matters in real programs.Russ
It would probably help to benchmark something more representative of the program you would like to write. This particular benchmark is measuring a cost that almost never matters in real programs.
Well, what I did was simply playing around a bit. It became public as I was asking this question about Nanoseconds(), but that doesn't incur that this fibonacci thing was intended to be serious. Nevertheless, I'm not sure whether the time a function call takes is that insignificant.
By my count, your fib(44) program makes 2,269,806,339 function calls, so Java is taking 3.5 nanoseconds per call, and Go is taking 6.2 nanoseconds per call.What I said was that it almost never matters in real programs, because real programs don't spend all their time making function calls: in a real program, the function being called does something, and that something will take significantly more than 6.2 nanoseconds.Russ
func fibImperative(n int) uint64 {
var x1 uint64 = 0
var x2 uint64 = 1
var tmp uint64 = 0
for i := 1; i <= n+1; i++ {
x1=x1+x2
tmp=x2
x2=x1
x1=tmp
}
return x1;
}
func main() {
var n = 44
var duration time.Duration
for i := 0; i < 50; i++ {
start := time.Now()
fibImperative(n)
duration = time.Now().Sub(start)
fmt.Println(duration)
}
}
private static long fib(long n) {
long x1 = 0;
long x2 = 1;
long tmp;
for(long i=1;i<=n+1;i++) {
x1=x1 + x2;
tmp=x2; x2=x1; x1=tmp;
}
return x1;
}
Just curious, is there a document about best practices for writing efficient (computationally fast) Go programs? Is the answer that the language is still really new, so wait for a while until the compiler is better / more stable?