我想測量我的相機的fps。我發現了一個簡單的代碼here。 如果我用codeBlocks(在Ununtu上)編譯代碼並運行循環600次,結果是27 fps。爲什麼我的代碼運行速度更快,如果我編譯codeBlocks
如果我從終端與編譯:
g++ -Wall main.cpp -o main -I/usr/local/include/ -lopencv_core -lopencv_highgui
結果爲14幀。爲什麼從終端編譯後這麼慢?
下面是代碼
#include "opencv2/opencv.hpp"
#include <time.h>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
// Start default camera
VideoCapture video(0);
// With webcam get(CV_CAP_PROP_FPS) does not work.
// Let's see for ourselves.
double fps;
// Number of frames to capture
int num_frames = 600;
// Start and end times
time_t start, end;
// Variable for storing video frames
Mat frame;
cout << "Capturing " << num_frames << " frames" << endl ;
// Start time
time(&start);
// Grab a few frames
for(int i = 0; i < num_frames; i++)
{
video >> frame;
}
// End Time
time(&end);
// Time elapsed
double seconds = difftime (end, start);
cout << "Time taken : " << seconds << " seconds" << endl;
// Calculate frames per second
fps = num_frames/seconds;
cout << "Estimated frames per second : " << fps << endl;
// Release video
video.release();
return 0;
}
CodeBlocks使用什麼編譯器,並使用什麼優化標誌?也不要在調試模式下測量任何東西。將-O2添加到命令行並重試。 – DeiDei
我在命令行中添加了-O2,但沒有任何更改。在codeBlocks中,GNU GCC編譯器被選中,只有標誌-g(產生debgging符號)被選中。設置是狡猾的,我沒有改變任何東西。 –
要比較編譯器的速度,您必須與產生最快二進制文件的選項進行比較。您需要在兩個編譯器中找到使代碼運行速度最快的選項,然後僅比較最快的運行。 – Dialecticus