编写计时程序的方法取决于你使用的编程语言。以下是几种常见编程语言的计时程序示例:
C语言
```c
include include int main() { clock_t start_time, end_time; double total_time; // 开始计时 start_time = clock(); // 运行需要计时的代码块 // ... // 结束计时 end_time = clock(); // 计算总时间 total_time = (double)(end_time - start_time) / CLOCKS_PER_SEC; printf("Total time: %f seconds\n", total_time); return 0; } ``` C++ ```cpp include include int main() { auto start_time = std::chrono::high_resolution_clock::now(); // 运行需要计时的代码块 // ... auto end_time = std::chrono::high_resolution_clock::now(); std::chrono::duration std::cout << "Total time: " << elapsed_time.count() << " seconds" << std::endl; return 0; } ``` Python ```python import time start_time = time.time() 运行需要计时的代码块 ... end_time = time.time() total_time = end_time - start_time print("Total time: ", total_time, " seconds") ``` Java ```java public class TimerExample { public static void main(String[] args) { long startTime = System.currentTimeMillis(); // 运行需要计时的代码块 // ... long endTime = System.currentTimeMillis(); long executionTime = endTime - startTime; System.out.println("程序执行时间: " + executionTime + " milliseconds"); } } ``` JavaScript (Node.js) ```javascript const start = Date.now(); // 运行需要计时的代码块 // ... const end = Date.now(); const totalTime = end - start; console.log("Total time: " + totalTime + " milliseconds"); ``` HTML + JavaScript (浏览器环境) ```html