#include <iostream>
#include <chrono>
#include <random>
int num=100000;
void pros(int *ab){
volatile int c=0;
auto start = std::chrono::high_resolution_clock::now();
for(int i=0;i<num;i++){
if(ab[i]&1){
c++;
}
}
auto end = std::chrono::high_resolution_clock::now();
auto diff = std::chrono::duration_cast<std::chrono::nanoseconds>(end - start);
std::cout << "c=" << c << ". " << diff.count() << " ナノ秒\n";
}
int main() {
std::random_device rd; // シード生成
std::mt19937 gen(rd()); // メルセンヌ・ツイスタ
std::uniform_int_distribution<> dist(1, 100);
int a[num],b[num];
for(int i=0;i<num;i++){
a[i] = i;
}
for(int i=0;i<num;i++){
b[i] = dist(gen);
}
pros(a);
pros(a);
pros(a);
pros(a);
pros(b);
pros(b);
pros(b);
pros(b);
}
To embed this program on your website, copy the following code and paste it into your website's HTML: