C语言--计算程序执行时间
C语言–计算程序执行时间
1. gettimeofday
精度1us
#include<stdio.h>
#include<sys/time.h>
int main()
{
/* 定义两个结构体 */
struct timeval start;
struct timeval end;
unsigned long timer;
/* 程序开始之前计时start */
gettimeofday(&start, NULL);
printf("hello world!\n");
/* 程序块结束后计时end */
gettimeofday(&end, NULL);
/* 统计程序段运行时间(unit is usec)*/
timer = 1000000 * (end.tv_sec - start.tv_sec) + end.tv_usec - start.tv_usec;
printf("timer = %ld us\n", timer);
return 0;
}
输出结果:
$ ./time
hello world!
timer = 47 us
2. clock
精度能达到1us
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
clock_t begin, end;
double cost;
//开始记录
begin = clock();
/*待测试程序段*/
printf("hello world!\n");
//结束记录
end = clock();
cost = (double)(end - begin)/CLOCKS_PER_SEC;
printf("constant CLOCKS_PER_SEC is: %ld, time cost is: %lf secs", CLOCKS_PER_SEC, cost);
}
输出结果www.cdxsxbx.com
$ ./time
hello world!
constant CLOCKS_PER_SEC is: 1000000, time cost is: 0.000042 secs
3. time命令
$ time ls
0.00s user 0.00s system 0% cpu 0.001 total
user时间是指进程花费在用户模式中的CPU时间,这是唯一真正用于执行进程所花费的时间,其他进程和花费阻塞状态中的时间没有计算在内。
sys时间是指花费在内核模式中的CPU时间,代表在内核中执系统调用所花费的时间,这也是真正由进程使用的CPU时间。
total指进程时间之和。精度1ms
C语言--计算程序执行时间的更多相关文章
- Java 获取并计算程序执行时间
一般输出日期时间经常会用到Date这个类: SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// ...
- C# stopwatch的简单使用(计算程序执行时间)
首先添加引用 using System.Diagnostics;//stopwatch的引用 //声明变量 Stopwatch a=new Stopwatch();//PS:这里一定要new(实例化) ...
- C语言计算程序运行时间
#include<stdio.h>#include<stdlib.h> #include "time.h" int main( void ) { ...
- R语言-程序执行时间
我们往往对自己编写程序的运行效率十分关心,需要查看程序的执行时间. 在R中,获得时间的函数有不少,比如system.time().proc.time()等. 个人使用较多的是proc.time() & ...
- c++中计算程序执行时间
#include<iostream> #include<time.h> using namespace std; int main() { clock_t t1 = clock ...
- linux c 编程 ------ 获取时间,计算程序执行时间
#include <time.h> #include <stdio.h> #include <unistd.h> int main(int argc, char a ...
- c语言统计程序执行时间
c语言程序执行时间 #include <iostream> #include <cstdio> #include <ctime> int main() { std: ...
- Python时钟,计算程序运行时间
关于计算程序执行时间 import time def sleep(): time.sleep(2.5) def forloop(count): for i in range(count): print ...
- Python小白学习之路(二十四)—【装饰器】
装饰器 一.装饰器的本质 装饰器的本质就是函数,功能就是为其他函数添加附加功能. 利用装饰器给其他函数添加附加功能时的原则: 1.不能修改被修饰函数的源代码 2.不能修改被修饰函数的调用 ...
随机推荐
- 如何下载Twitter视频?最简单的保存推特视频的方法
Twitter上面的短视频越来越流行了,但是推特官方并没有提供下载通道.如果你想下载这些小视频到电脑或者手机(安卓/iPhone),该如何操作呢?下面介绍一种最简单的方法. 下载Twitter视频我们 ...
- 第一篇:C++之hello world
1.编辑器:Microsoft Visual C++ 2010,下载安装 2.新建项目 代码: #include <iostream>#include <Windows.h>/ ...
- Windows鼠标右键新建中增加新建md文件
1.新建一个文本文件,拷贝一下内容到其中: Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\.md\ShellNew] "Nu ...
- 前端深入之js篇丨Array数组操作从入门到成神Up Up Up,持续更新中
写在前面 随着前端深入的不断学习,发现数组这个数据结构在前端中有着相当大的存在感,由于我初学前端的时候并没有系统性的学习数组,所以我将通过这篇文章同你一起学习数组,希望我们能一起进步,学会熟练操作数组 ...
- docker: manifest for elasticsearch:latest not found
今天在docker安装es出现坑,是这样. 使用: docker pull elasticsearch 提示:manifest for elasticsearch:latest not found如图 ...
- Add a Parametrized Action 添加带参数的按钮
In this lesson, you will learn how to add a Parametrized Action. These types of Actions are slightly ...
- Python:多态、协议和鸭子类型
多态 问起面向对象的三大特性,几乎每个人都能对答如流:封装.继承.多态.今天我们就要来说一说 Python 中的多态. 所谓多态:就是指一个类实例的相同方法在不同情形有不同表现形式.多态机制使具有不同 ...
- FCC---Use the CSS Transform scale Property to Scale an Element on Hover
The transform property has a variety of functions that let you scale, move, rotate, skew, etc., your ...
- vue-cli 引用elementUI打包后文件过大
解决方案:使用externals引用第三方资源,防止element资源被打包到自己项目中,(总共修改3个页面index.html.webpack.base.conf.js.main.js) 1.修改i ...
- 射频IC卡和IC卡读卡器的成本分析
当今射频IC卡和IC卡读卡器的种类繁多,很多人问IC卡读卡器多少钱,那么如何在满足我们需求的情况下最大的节省成本呢.下面就各种射频IC卡和IC卡读卡器来分析下各自的成本. ...