Linux获取毫秒级时间
Linux获取毫秒级时间
Moakap
在软件设计中经常会用到关于时间的处理,用来计算语句、函数的执行时间,这时就需要精确到毫秒甚至是微妙的时间。
int gettimeofday(struct
timeval *tv, struct timezone *tz);
int settimeofday(const
struct timeval *tv , const struct timezone *tz);
struct timeval {
time_t tv_sec; /* seconds */
suseconds_t tv_usec; /*
microseconds */
};
struct timezone {
int tz_minuteswest; /* minutes
west of Greenwich
*/
int
tz_dsttime; /* type of DST
correction */
};
下面是个简单的例子,用来统计程序的执行时间:
…
struct timeval
t_start,t_end;
long cost_time = 0;
//get start
time
gettimeofday(&t_start,
NULL);
printf("Start
time: %ld us", t_start.tv_usec);
//some
operation
…
//get end time
gettimeofday(&t_end,
NULL);
printf("End
time: %ld us", t_end.tv_usec);
//calculate
time slot
cost_time =
t_end.tv_usec - t_start.tv_usec;
printf("Cost
time: %ld us", cost_time);
…
输出:
Start time:
438061 us
End time:
459867 us
Cost time:
21806 us
相关文档:
linux中,在支持多线程的环境中,通常每个线程都有属于自己的errno变量,是用来表示特定错误的常量。
以下是<errno.h>中定义的所有出错errno常量
#define EPERM 1 /* Operation not permitted */
#define ENOEN ......
shell语法(五项)
1.命令格式
2.通配符
3.重定向
4.管道
5.shell中的引用
6.自动补齐命令行
系统管理维护
ls
pwd
cd
date
passwd
su
clear
man
who
w
uname
uptime
last
dmesg
free
ps
top
文件管理编辑
mkdir
more
cat
diff
grep
rm
touch
ln
file
cp
find
split
mv
压缩解压
zi ......
1,显著影响系统性能的4种资源
(1),CPU时间
(2),内存
(3),硬盘I/O
(4),网络I/O
2,分析CPU使用情况
使用vmstat采集CPU的性能瓶颈
[root@local]# vmstat 2
procs -----------memory---------- ---swap-- -----io-- ......