일부 정렬 및 검색 알고리즘을 작성하고 대학 과제를 테스트하면서 다른 테스트를 처리하는 데 사용 된 CPU 시간과 벽 시간을 가져야합니다. 개별 타이밍뿐만 아니라.boost :: timer 결과가 변수에 저장됩니다.
나는 이것을 달성하기 위해 부스트 API를 사용하고 있는데, 내 문제는 여러 테스트를 실행하고 평균 시간을 가져야한다는 것이지만, 결과를 저장하는 솔루션을 찾을 수 없다. . @Surt I'v은 다음과 같이 코드를 구현하려
int CA1::binarySearch(vector<int> v, int target)
{
boost::timer::auto_cpu_timer t("%w");
int top, bottom, middle;
top = vecSize - 1;
bottom = 0;
while (bottom <= top)
{
middle = (top + bottom)/2;
if (v[middle] == target)
return middle;
else if (v[middle] > target)
top = middle - 1;
else
bottom = middle + 1;
}
return -1;
}
편집 : 여기
내 알고리즘 중 하나입니다
int main()
{
int size = 0;
cout << " enter the size of your vector\n";
cin >> size;
CA1 ca1(size);
ca1.DoTests;
system("pause");
return 0;
}
int CA1::binarySearch(vector<int> v, int target)
{
int top, bottom, middle;
top = vecSize - 1;
bottom = 0;
while (bottom <= top)
{
middle = (top + bottom)/2;
if (v[middle] == target)
return middle;
else if (v[middle] > target)
top = middle - 1;
else
bottom = middle + 1;
}
return -1;
}
double measure(std::function<void()> function) {
auto start_time = std::chrono::high_resolution_clock::now();
function();
auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>
(std::chrono::high_resolution_clock::now() - start_time);
//std::cout << test << " " << static_cast<double>(duration.count()) * 0.000001 <<
// " ms" << std::endl;
return static_cast<double>(duration.count()) * 0.000001;
}
void CA1::DoTests() {
double time = measure(CA1::binarySearch(vectorUnordered,2));
cout << time << endl;
}
그러나 나는군요 오류가 발생했습니다.
error C3867: 'CA1::DoTests': function call missing argument list; use '&CA1::DoTests' to create a pointer to member
functional(228) : see reference to function template instantiation '_Ret std::_Callable_obj<int,false>::_ApplyX<_Rx,>(void)' being compiled
내가 잘못한 생각이 있으십니까?
편집 2
@ 롭 케네디
다음과 같이 내 코드를 변경했습니다 난 당신의 코드를 표준 : : 바인딩을 구현하기 위해 노력했습니다하지만, 주위 을 내 머리를 얻을 어차피: 던져
double CA1::measure(std::function<void()> function) {
auto startCpu = boost::chrono::process_real_cpu_clock::now();
auto startWall = boost::chrono::process_system_cpu_clock::now();
function();
auto durationCpu = boost::chrono::duration_cast<boost::chrono::nanoseconds>
(boost::chrono::process_real_cpu_clock::now() - startCpu);
auto durationWall = boost::chrono::duration_cast<boost::chrono::nanoseconds>
(boost::chrono::process_system_cpu_clock::now() - startWall);
double cpuTime = static_cast<double>(durationCpu.count()) * 0.000001;
double wallTime = static_cast<double>(durationWall.count()) * 0.000001;
/*return static_cast<double>(duration.count()) * 0.000001;*/
cout << "Cpu time " << cpuTime << endl;
cout << "Wall time " << wallTime << endl;
return cpuTime;
}
void CA1::DoTests() {
auto time = measure(std::bind(binarySearch, vectorUnordered, 2));
}
오류 :
error C3867: 'CA1::binarySearch': function call missing argument list; use '&CA1::binarySearch' to create a pointer to member
나는 std :: bind를 올바른 위치에두고 있습니까? measure()에서 매개 변수를 변경해야합니까? 정확히 무엇을하고 있습니까?
오류 메시지를 당신은 타이머와 전혀 관련이없는 것을보고 있습니다. 당신은 * 함수 *를 당신의'measure' 함수에 전달하지 않습니다; 'int '- 당신이 *'binarySearch'를 호출했을 때 반환되는 것을 전달하고 있습니다. –
@johntk,'ca1.DoTests;'는'ca1.DoTests'()이어야합니다; – Surt
@Rob Kennedy 이제 리턴 값을 가진 이와 같은 함수를 전달할 수있는 방법이 있습니까? – Johntk