何も機能しません。関数呼び出しでの単純な整数のインクリメントでさえありません。
ヘッダーファイルに次のように表示されると思われます。
static clock_t t1, total;
その場合、各翻訳単位は 2 つの変数の独自の個別のインスタンスを取得します (おかげでstatic
)。
修正するには、ヘッダーを に変更static
しextern
、.cpp ファイルに次を追加します。
clock_t t1, total;
これを示すEDITサンプルに従う:
OPのリクエストによると、これはテンプレートコンパレーターとこの回答のレシピを使用して、実行中のクロックの合計を宣言および管理する短い例です。
main.h
#ifndef PROJMAIN_DEFINED
#define PROJMAIN_DEFINED
extern clock_t total;
template<typename T>
bool less_default(const T& left, const T& right)
{
clock_t t1 = clock();
bool res = (left < right);
total += (clock() - t1);
return res;
};
#endif
main.cpp
#include <iostream>
#include <algorithm>
#include <iterator>
#include <vector>
#include "main.h"
using namespace std;
clock_t total = 0;
int main()
{
static const size_t N = 2048;
vector<int> values;
values.reserve(N);
std::srand((unsigned)time(0));
cout << "Generating..." << endl;
generate_n(back_inserter(values), N, [](){ static int i=0; return ++i;});
for (int i=0;i<5;++i)
{
random_shuffle(values.begin(), values.end());
cout << "Sorting ..." << endl;
total = 0;
std::sort(values.begin(), values.end(), less_default<int>);
cout << "Finished! : Total = " << total << endl;
}
return EXIT_SUCCESS;
}
出力
Generating...
Sorting ...
Finished! : Total = 13725
Sorting ...
Finished! : Total = 13393
Sorting ...
Finished! : Total = 15400
Sorting ...
Finished! : Total = 13830
Sorting ...
Finished! : Total = 15789