私は 2 つのプログラムを担当しており、これは 2 つ目のプログラムです。最初のプログラムには、calculation() 関数が含まれておらず、プログラムの開始時と終了時に時間を計っていました。私のコンピュータは、0.523 秒から 0.601 秒まで何でも表示します。
2 番目のタスクは、計算用のインライン関数を作成することでした。高速ではないため、間違っていると思います。表示情報が含まれているために計算関数を正しく作成したのか、それともインライン関数が乗算のみに焦点を当てるべきなのかはわかりません。どちらの方法でも、配列をメインから取り出して関数に入れるのは速くありません。
コンパイラはそれを無視していますか?
#include <ctime>
#include <iostream>
using namespace std;
inline int calculation(){
int i;
double result[10000];
double user[10000];
for(i=0; i<10000; i++){
user[i]=i+100;
}
double second[10000];
for(i=0; i<10000; i++){
second[i]=10099-i;
}
for (i = 0; i < 10000; i++){
result[i] = user[i] * second[i];
}
for (i = 0; i < 10000; i++){
cout << user[i] << " * " << second[i] << " = " << result[i] << '\n';
}
}
int main() {
time_t t1 = time(0); // get time now
struct tm * now = localtime( & t1 );
cout << "The time now is: ";
cout << now->tm_hour << ":" << now->tm_min << ":" << now->tm_sec << endl;
clock_t t; // get ticks
t = clock();
cout << " Also calculating ticks...\n"<<endl;
calculation(); // inline function
time_t t2 = time(0); // get time now
struct tm * now2 = localtime( & t2 );
cout << "The time now is: ";
cout << now2->tm_hour << ":" << now2->tm_min << ":" << now2->tm_sec << endl;
time_t t3= t2-t1;
cout << "This took me "<< t3 << " second(s)" << endl; // ticks
t = clock() - t;
float p;
p = (float)t/CLOCKS_PER_SEC;
cout << "Or more accuratley, this took " << t << " clicks"
<< " or " << p << " seconds"<<endl;
}