Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <functional>
- #include <cstring>
- using namespace std;
- double test0(int count, int values[]) {
- int* m = new int[count];
- double start = clock();
- for (int i = 0; i < count; ++i) {
- for (int j = 0; j < i; ++j) {
- m[j + 1] = m[j];
- }
- m[0] = values[i];
- }
- double end = clock();
- delete m;
- return (end - start) / CLOCKS_PER_SEC;
- }
- double test1(int count, int values[]) {
- int* m = new int[count];
- double start = clock();
- int bytes = sizeof(m[0]);
- int bytesInArray = 0;
- for (int i = 0; i < count; ++i) {
- memmove(m + 1, m, bytesInArray);
- m[0] = values[i];
- bytesInArray += bytes;
- }
- double end = clock();
- delete m;
- return (end - start) / CLOCKS_PER_SEC;
- }
- double test2(int count, int values[]) {
- vector<int> m;
- double start = clock();
- for (int i = 0; i < count; ++i) {
- m.push_back(values[i]);
- }
- double end = clock();
- return (end - start) / CLOCKS_PER_SEC;
- }
- int main() {
- vector<int> counts = {
- 10000,
- 50000,
- 100000,
- 500000,
- 1000000,
- 5000000
- };
- int max_count = counts.back();
- int* v = new int[max_count];
- for (int i = 0; i < max_count; ++i) {
- v[i] = rand();
- }
- vector<function<double(int, int[])>> tests = {
- test0,
- test1,
- test2
- };
- for (auto count : counts) {
- printf("count=%d\n", count);
- for (int i = 0; i < tests.size(); ++i) {
- printf("\ttest #%d\t: \t%.10lfsec\n", i, tests[i](count, v));
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement