The method of calculating the time-consuming in microseconds (including: hours/minutes/seconds/milliseconds/microseconds/nanoseconds)

Method 1 that takes microseconds to calculate

#include<stdio.h>
#include <windows.h>
int main() {
	int a[10002];
	int i = 0;
	double run_time;
	_LARGE_INTEGER time_start;	//开始时间
	_LARGE_INTEGER time_over;	//结束时间
	double dqFreq;		//计时器频率
	LARGE_INTEGER f;	//计时器频率
	QueryPerformanceFrequency(&f);
	dqFreq=(double)f.QuadPart;
	QueryPerformanceCounter(&time_start);	//计时开始
	for( i = 1; i <= 10000; i++)a[i]=i;	//要计时的程序
	QueryPerformanceCounter(&time_over);	//计时结束
	run_time=1000000*(time_over.QuadPart-time_start.QuadPart)/dqFreq;
	//乘以1000000把单位由秒化为微秒,精度为1000 000/(cpu主频)微秒
	printf("\nrun_time:%fus\n",run_time);
	return 0;
}

Method 2 that takes microseconds to calculate

#include <QDateTime>

int main()
{
    // 获取当前时间
    QDateTime startTime = QDateTime::currentDateTime();

    // 执行需要计时的代码
    // ...

    // 获取当前时间
    QDateTime endTime = QDateTime::currentDateTime();

    // 计算执行时间
    qint64 elapsedTime = startTime.msecsTo(endTime);
    qDebug() << "Elapsed time: " << elapsedTime << " microseconds";
    return 0;
}

In the above code,  QDateTime the class is used to obtain the current time, then calculate the execution time, and finally output the result. msecsTo() The time difference at the millisecond level can be obtained by using  the method, and then it can be converted to the microsecond level.

Method 3--QTime

#include <QTime>

// 计算代码执行时间并返回微秒
int calcExecTime() {
    QTime timer;
    timer.start();
    // 这里放需要计时的代码
    // ...
    return timer.elapsed();
}

// 示例代码
int main() {
    int elapsed = calcExecTime();
    qDebug() << "代码执行时间:" << elapsed << "微秒";
    return 0;
}

In the above code, we used Qt's QTime class to calculate the code execution time. In the function calcExecTime(), we use timer.start()start timing, then run the code that needs to be timed, and finally use timer.elapsed()the return code to execute the time. In the main function, we call calcExecTime()and print out the code execution time.

Method 4---Use the chrono library in the C++ standard library to calculate time-consuming

#include <iostream>
#include <chrono>

int main() {
    auto start = std::chrono::high_resolution_clock::now(); // 记录开始时间

    // 执行需要计时的代码
    int sum = 0;
    for (int i = 0; i < 1000000; ++i) {
        sum += i;
    }

    auto end = std::chrono::high_resolution_clock::now(); // 记录结束时间
    auto duration = std::chrono::duration_cast<std::chrono::microseconds>(end - start); // 计算耗时

    std::cout << "耗时:" << duration.count() << "微秒" << std::endl; // 输出耗时
    return 0;
}

in:

std::chrono::high_resolution_clockIt is a high-precision clock provided by the C++ standard library, which can measure time at the level of microseconds and nanoseconds.

std::chrono::duration_castThe function converts the time difference to the length of time in the specified unit.

In the above code, startand endrespectively record the start and end time of the code that needs timing. After calculating the time difference, duration.count()obtain the total time-consuming, in microseconds.

Notice:

If you don't want to use auto start/end, you can change it to

std::chrono::time_point<std::chrono::steady_clock> start=std::chrono::high_resolution_clock::now();

std::chrono::time_point<std::chrono::steady_clock> end=std::chrono::high_resolution_clock::now();

Method 5-QT uses C++ language to calculate time-consuming sample codes. Use QElapsedTimer to calculate the execution time of the code block, accurate to microseconds

#include <QCoreApplication>
#include <QElapsedTimer>
#include <iostream>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QElapsedTimer timer;
    timer.start();

    // 计算代码块的执行时间
    long long sum = 0;
    for (int i = 0; i < 100000000; i++) {
        sum += i;
    }

    // 输出执行时间,单位为微秒
    std::cout << "Time elapsed: " << timer.nsecsElapsed() / 1000 << "us" << std::endl;

    return a.exec();
}

In this example, we have used a for loop to calculate the sum of all integers from 0 to 99999999. After the calculation is complete, we use nsecsElapsed()the function to get the execution time of the code block in nanoseconds, and divide it by 1000 to convert it to microseconds.

Among them, the QElapsedTimer class can be found in the header file <QElapsedTimer>, and the usage method is as follows:

  1. Call the start() method to start timing;
  2. perform time-consuming operations;
  3. Call the nsecsElapsed() method to obtain the time-consuming time (in nanoseconds);
  4. Divide the number of nanoseconds by 1000 to get the number of microseconds.

Finally, just output the calculated microseconds.

Guess you like

Origin blog.csdn.net/weixin_56819992/article/details/131080811