C++打印函数耗时

打印時間差值1

#include <chrono>
#include <iostreeam>

int main()
{
	auto start = std::chrono::steady_clock::now();
	// Do something 
	auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start);
	// std::chrono::milliseconds可以按特定單位換爲下列type
	printf("get item volume agent call: it took %f", (double) duration.count());
	return 0;
}

C++ print2

Convenience duration typedefs
Defined in namespace std::chrono


std::chrono::nanoseconds(C++11) 	duration type with Period std::nano
std::chrono::microseconds(C++11) 	duration type with Period std::micro
std::chrono::milliseconds(C++11) 	duration type with Period std::milli
std::chrono::seconds(C++11) 	duration type with Period std::ratio<1>
std::chrono::minutes(C++11) 	duration type with Period std::ratio<60>
std::chrono::hours(C++11) 	duration type with Period std::ratio<3600>
std::chrono::days(C++20) 	duration type with Period std::ratio<86400>
std::chrono::weeks(C++20) 	duration type with Period std::ratio<604800>
std::chrono::months(C++20) 	duration type with Period std::ratio<2629746>
std::chrono::years(C++20) 	duration type with Period std::ratio<31556952>

python打印函数耗时

import time

def main():
	start_time = time.time()
	time.sleep(1)
	end_time = time.time()
	cost_time = stop_time - start_time
    print("Cost time: {} second.".format(cost_time ))

if __name__ == "__main__":
    main()

打印输出当前时间可参考:
c++ printf 当前时间


  1. c++ chrono reference() ↩︎

  2. c++ printf reference ↩︎

Logo

GitCode 天启AI是一款由 GitCode 团队打造的智能助手,基于先进的LLM(大语言模型)与多智能体 Agent 技术构建,致力于为用户提供高效、智能、多模态的创作与开发支持。它不仅支持自然语言对话,还具备处理文件、生成 PPT、撰写分析报告、开发 Web 应用等多项能力,真正做到“一句话,让 Al帮你完成复杂任务”。

更多推荐