2014-10-22 48 views
0

我正在寫一些某種形式和搜索算法和測試他們爲大學分配,我得的CPU時間和掛鐘時間在處理不同的測試中使用的量。以及個人時間。商店的boost ::計時器結果在一個變量

我使用升壓API來實現這一目標,我的問題是,我必須運行多個測試並拿到平均時間,但我找不到存儲結果升壓解決方案中的變量在我給。

這裏是我的算法之一:

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; 
} 

編輯

@Surt心中已經想實現你的代碼如下:

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

@Rob肯尼迪

我試圖執行代碼的std ::綁定,但我不能讓我的頭周圍, 我已經改變了我的代碼如下:

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()中的參數? 它到底在做什麼?

+1

錯誤消息你所看到的與計時器完全無關。你沒有向你的'measure'函數傳遞*函數*;你傳遞一個'int' - 當你調用*'binarySearch'時返回的那個。 – 2014-10-22 22:59:42

+1

@johntk,'ca1.DoTests;'應該是'ca1.DoTests'(); – Surt 2014-10-22 23:09:37

+0

@Rob Kennedy我現在看到,有沒有什麼辦法可以傳遞這樣的函數,它具有返回值? – Johntk 2014-10-23 09:40:04

回答

1

你使用boost::timer::auto_cpu_timer而是應該選用boost::timer::cpu_timer血淋淋的細節here

爲了方便測量使用這樣的事情,只是在std交換自己喜歡的定時器功能::計時細節:

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); 

    return static_cast<double>(duration.count()) * 0.000001; 
} 

void Test1() { 
    ... setup test 
    ... call test 
    ... validate return 
} 

void DoTests() { 
    double time = measure(Test1); 
    ... 
    ... profit! 
} 
+0

好,所以我想我理解你的代碼糾正了我對我的錯誤, 所以,你創建了一個名爲measure的方法,它返回一個double並接受一個字符串和一個函數作爲參數, 然後你啓動計時器, 調用傳入函數, 獲取運行時間,將其轉換爲double並返回結果。 假設這是正確的,糾正我,如果它錯了,爲什麼你傳遞字符串?我們已經嘗試了您的代碼 void CA1 :: DoTests(){ \t double time = measure(「Test1」,CA1 :: binarySearch(vectorUnordered,2)); \t \t cout << time << endl; } – Johntk 2014-10-22 22:27:01

+1

@johntk,measure會返回MS中使用的時間,但是您的系統計時器可能並不準確,請參閱頂部的鏈接。這個想法是,你不會污染你想用儀器測量的功能,並可以重新使用定時代碼。 – Surt 2014-10-22 22:33:36

+0

這裏不需要字符串,您可以刪除該代碼。 – Surt 2014-10-22 22:34:35