Skip to content

Instantly share code, notes, and snippets.

@monokuma201607
Created February 13, 2022 20:39
Show Gist options
  • Save monokuma201607/1770c6288606589f31f48032835e05d5 to your computer and use it in GitHub Desktop.
Save monokuma201607/1770c6288606589f31f48032835e05d5 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <vector>
#include <algorithm>
#include <Windows.h>
#include <MyStudyConsoleApplication.h>
using namespace std;
//for_each用関数
void Hoge(int x)
{
int a = x;
}
void for_ex1() {
vector<int> array;
for (int i = 0; i < MAX; ++i) {
array.push_back(i);
}
LARGE_INTEGER freq;
QueryPerformanceFrequency(&freq);
LARGE_INTEGER start, end;
printf("初期化Int型でサイズ文回す \n");
QueryPerformanceCounter(&start);
for (int x : array) {
int a = x;
}
QueryPerformanceCounter(&end);
double time = static_cast<double>(end.QuadPart - start.QuadPart) * 1000.0 / freq.QuadPart;
printf("time %lf[ms]\n", time);
}
void for_ex2() {
vector<int> array;
for (int i = 0; i < MAX; ++i) {
array.push_back(i);
}
LARGE_INTEGER freq;
QueryPerformanceFrequency(&freq);
LARGE_INTEGER start, end;
printf("初期化Auto型でサイズ文回す \n");
QueryPerformanceCounter(&start);
for (auto x : array) {
int a = x;
}
QueryPerformanceCounter(&end);
double time = static_cast<double>(end.QuadPart - start.QuadPart) * 1000.0 / freq.QuadPart;
printf("time %lf[ms]\n", time);
}
void for_ex3() {
vector<int> array;
for (int i = 0; i < MAX; ++i) {
array.push_back(i);
}
LARGE_INTEGER freq;
QueryPerformanceFrequency(&freq);
LARGE_INTEGER start, end;
printf("初期化int型でカウント回し \n");
QueryPerformanceCounter(&start);
int count = 0;
for (int i = 0; i < array.size();i++) {
int a = array[count++];
}
QueryPerformanceCounter(&end);
double time = static_cast<double>(end.QuadPart - start.QuadPart) * 1000.0 / freq.QuadPart;
printf("time %lf[ms]\n", time);
}
void for_ex4() {
vector<int> array;
for (int i = 0; i < MAX; ++i) {
array.push_back(i);
}
LARGE_INTEGER freq;
QueryPerformanceFrequency(&freq);
LARGE_INTEGER start, end;
printf("初期化イテレータで終わりまで \n");
QueryPerformanceCounter(&start);
int count = 0;
for (vector<int>::iterator i = array.begin(); i != array.end(); i++) {
int a = *i;
}
QueryPerformanceCounter(&end);
double time = static_cast<double>(end.QuadPart - start.QuadPart) * 1000.0 / freq.QuadPart;
printf("time %lf[ms]\n", time);
}
void for_ex5() {
vector<int> array;
for (int i = 0; i < MAX; ++i) {
array.push_back(i);
}
LARGE_INTEGER freq;
QueryPerformanceFrequency(&freq);
LARGE_INTEGER start, end;
printf("foreach \n");
QueryPerformanceCounter(&start);
int count = 0;
for_each(array.begin(), array.end(), Hoge);
QueryPerformanceCounter(&end);
double time = static_cast<double>(end.QuadPart - start.QuadPart) * 1000.0 / freq.QuadPart;
printf("time %lf[ms]\n", time);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment