Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
9
Task/Time-a-function/Go/time-a-function-1.go
Normal file
9
Task/Time-a-function/Go/time-a-function-1.go
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
package empty
|
||||
|
||||
func Empty() {}
|
||||
|
||||
func Count() {
|
||||
// count to a million
|
||||
for i := 0; i < 1e6; i++ {
|
||||
}
|
||||
}
|
||||
15
Task/Time-a-function/Go/time-a-function-2.go
Normal file
15
Task/Time-a-function/Go/time-a-function-2.go
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
package empty
|
||||
|
||||
import "testing"
|
||||
|
||||
func BenchmarkEmpty(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
Empty()
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkCount(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
Count()
|
||||
}
|
||||
}
|
||||
31
Task/Time-a-function/Go/time-a-function-3.go
Normal file
31
Task/Time-a-function/Go/time-a-function-3.go
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func empty() {}
|
||||
|
||||
func count() {
|
||||
for i := 0; i < 1e6; i++ {
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
e := testing.Benchmark(func(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
empty()
|
||||
}
|
||||
})
|
||||
c := testing.Benchmark(func(b *testing.B) {
|
||||
for i := 0; i < b.N; i++ {
|
||||
count()
|
||||
}
|
||||
})
|
||||
fmt.Println("Empty function: ", e)
|
||||
fmt.Println("Count to a million:", c)
|
||||
fmt.Println()
|
||||
fmt.Printf("Empty: %12.4f\n", float64(e.T.Nanoseconds())/float64(e.N))
|
||||
fmt.Printf("Count: %12.4f\n", float64(c.T.Nanoseconds())/float64(c.N))
|
||||
}
|
||||
25
Task/Time-a-function/Go/time-a-function-4.go
Normal file
25
Task/Time-a-function/Go/time-a-function-4.go
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"time"
|
||||
)
|
||||
|
||||
func from(t0 time.Time) {
|
||||
fmt.Println(time.Now().Sub(t0))
|
||||
}
|
||||
|
||||
func empty() {
|
||||
defer from(time.Now())
|
||||
}
|
||||
|
||||
func count() {
|
||||
defer from(time.Now())
|
||||
for i := 0; i < 1e6; i++ {
|
||||
}
|
||||
}
|
||||
|
||||
func main() {
|
||||
empty()
|
||||
count()
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue