我必须以毫秒为单位计算算法的速度.在C/C++中,我该怎么做?我需要在输入之前和输出之后写smth但是究竟是什么?
解决方法
您可以使用< time.h>中的clock()函数.
clock()显示自程序启动以来已经过了多少个刻度.宏CLOCKS_PER_SEC包含每秒的滴答数,因此您实际上可以获得时间.
clock()显示自程序启动以来已经过了多少个刻度.宏CLOCKS_PER_SEC包含每秒的滴答数,因此您实际上可以获得时间.
//We start measuring here. Remember what was the amount of ticks in the //beginning of the part of code you want to test: int start = clock(); //<...> //Do your stuff here //<...> int end = clock();//Now check what amount of ticks we have Now. //To get the time,just subtract start from end,and divide by CLOCKS_PER_SEC. std::cout << "it took " << end - start << "ticks,or " << ((float)end - start)/CLOCKS_PER_SEC << "seconds." << std::endl;