实验环境
Kernel Version: 4.15.0-66-generic
Operating System: Ubuntu 18.04.4 LTS
OSType: linux
Architecture: x86_64
CPUs: 1
Total Memory: 1.947GiB
测试代码
创建一个 1M 的文件
dd if=/dev/zero of=in.txt bs=1M count=1
系统调用测试代码
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
char c;
int in;
int i;
in = open("in.txt", O_RDONLY);
for(i=0; i<1000000; i++){ // 一百万次系统调用
read(in,&c,1);
}
return 0;
}
时间统计
root@aliyun:c-code# time ./read-syscall
real 0m0.746s
user 0m0.388s
sys 0m0.340s
由上,一百万次 read 系统调用,耗时 0.340s。也就是一次系统调用是 0.34 微秒,也就是 340 纳秒。一次系统调用大概是几百纳秒。
本文通过在Ubuntu18.04环境下,使用C语言实现的系统调用测试代码,详细分析了read系统调用的性能。实验显示,在1M大小的文件上进行一百万次read调用,平均每次调用耗时约340纳秒。
1047

被折叠的 条评论
为什么被折叠?



