Created
November 16, 2014 11:27
-
-
Save coldcue/670c3317dbb1d56ca762 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#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; | |
} |
This file contains hidden or 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
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