Skip to content

Instantly share code, notes, and snippets.

@coldcue
Created November 16, 2014 11:27
Show Gist options
  • Save coldcue/670c3317dbb1d56ca762 to your computer and use it in GitHub Desktop.
Save coldcue/670c3317dbb1d56ca762 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <thread>
#include <ctime>
using namespace std;
void calc(unsigned long iterations) {
unsigned long n = 0;
for (unsigned long i = 0; i < iterations; i++) {
n = n + n * (i % 5);
}
}
clock_t test() {
clock_t start = clock();
thread *threads[8];
for (int i = 0; i < 8; i++) {
threads[i] = new thread(calc, 1024 * 1024 * 1024);
}
for (int i = 0; i < 8; i++) {
(*threads[i]).join();
}
return clock() - start;
}
int main() {
cout << "Time: " << ((double) test()) / CLOCKS_PER_SEC * 1000 << endl;
return 0;
}
package main
import (
"fmt"
"syscall"
"sync"
)
func main() {
var wg sync.WaitGroup
var start syscall.Timeval
syscall.Gettimeofday(&start)
for i := 0; i < 8; i++ {
wg.Add(1)
go func(iterations int) {
defer wg.Done()
n := 0
for i := 0; i < iterations; i++ {
n = n + n * (i % 5)
}
}(1024 * 1024 * 1024)
}
wg.Wait()
var end syscall.Timeval
syscall.Gettimeofday(&end)
fmt.Printf("Time: %d \n", int((end.Usec-start.Usec) / 1000))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment