-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
article: move assets to bench-time folder
- Loading branch information
Showing
10 changed files
with
111 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
{ | ||
"files.associations": { | ||
"*.go2": "go", | ||
"chrono": "cpp" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package time_test | ||
|
||
import ( | ||
"fmt" | ||
"sync/atomic" | ||
"testing" | ||
"time" | ||
) | ||
|
||
func BenchmarkAtomic(b *testing.B) { | ||
var v int32 | ||
var n = 1000000 | ||
for k := 1; k < n; k *= 10 { | ||
b.Run(fmt.Sprintf("n-%d", k), func(b *testing.B) { | ||
atomic.StoreInt32(&v, 0) | ||
b.Run("with-timer", func(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
b.StopTimer() | ||
b.StartTimer() | ||
for j := 0; j < k; j++ { | ||
atomic.AddInt32(&v, 1) | ||
} | ||
} | ||
}) | ||
atomic.StoreInt32(&v, 0) | ||
b.Run("w/o-timer", func(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
for j := 0; j < k; j++ { | ||
atomic.AddInt32(&v, 1) | ||
} | ||
} | ||
}) | ||
}) | ||
} | ||
} | ||
|
||
func TestSolution(t *testing.T) { | ||
var v int32 | ||
atomic.StoreInt32(&v, 0) | ||
r := testing.Benchmark(func(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
b.StopTimer() | ||
// ... do extra stuff ... | ||
b.StartTimer() | ||
atomic.AddInt32(&v, 1) | ||
} | ||
}) | ||
|
||
// do calibration that removes the overhead of calling time.Now(). | ||
calibrate := func(d time.Duration, n int) time.Duration { | ||
since := time.Duration(0) | ||
for i := 0; i < n; i++ { | ||
start := time.Now() | ||
since += time.Since(start) | ||
} | ||
return (d - since) / time.Duration(n) | ||
} | ||
|
||
fmt.Printf("%v ns/op\n", calibrate(r.T, r.N)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#include <iostream> | ||
#include <chrono> | ||
|
||
void empty() {} | ||
|
||
int main() { | ||
int n = 1000000; | ||
for (int j = 0; j < 10; j++) { | ||
std::chrono::nanoseconds since(0); | ||
for (int i = 0; i < n; i++) { | ||
auto start = std::chrono::steady_clock::now(); | ||
empty(); | ||
since += std::chrono::steady_clock::now() - start; | ||
} | ||
std::cout << "avg since: " << since.count() / n << "ns \n"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#include <iostream> | ||
#include <chrono> | ||
|
||
void target() {} | ||
|
||
int main() { | ||
int n = 1000000; | ||
for (int j = 0; j < 10; j++) { | ||
std::chrono::nanoseconds since(0); | ||
for (int i = 0; i < n; i++) { | ||
auto start = std::chrono::steady_clock::now(); | ||
target(); | ||
since += std::chrono::steady_clock::now() - start; | ||
} | ||
|
||
auto overhead = -(std::chrono::steady_clock::now() - | ||
std::chrono::steady_clock::now()); | ||
since -= overhead * n; | ||
|
||
std::cout << "avg since: " << since.count() / n << "ns \n"; | ||
} | ||
} |
File renamed without changes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module benchtime | ||
|
||
go 1.16 |
File renamed without changes
File renamed without changes
File renamed without changes.