Created
August 21, 2012 21:12
-
-
Save bnoordhuis/3419436 to your computer and use it in GitHub Desktop.
test ioctl() vs fcntl() syscall speed
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// cc -O fioclex.c | |
// USE_IOCTL=0 time ./a.out | |
// USE_IOCTL=1 time ./a.out | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <sys/ioctl.h> | |
#include <unistd.h> | |
#include <fcntl.h> | |
#ifndef __linux__ | |
# include <sys/filio.h> | |
#endif | |
int main(int argc, char **argv) { | |
int use_ioctl; | |
int fd; | |
int i; | |
if (argv[1]) | |
fd = open(argv[1], O_RDONLY); | |
else | |
fd = STDERR_FILENO; | |
if (fd == -1) | |
return puts("open error"), EXIT_FAILURE; | |
use_ioctl = getenv("USE_IOCTL") && atoi(getenv("USE_IOCTL")); | |
for (i = 0; i < 10e6; i++) { | |
if (use_ioctl) { | |
if (ioctl(fd, FIOCLEX)) | |
abort(); | |
} | |
else { | |
if (fcntl(fd, F_SETFD, FD_CLOEXEC)) | |
abort(); | |
} | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// cc -O fionbio.c | |
// USE_IOCTL=0 time ./a.out | |
// USE_IOCTL=1 time ./a.out | |
#include <stdio.h> | |
#include <stdlib.h> | |
#include <sys/ioctl.h> | |
#include <unistd.h> | |
#include <fcntl.h> | |
#ifndef __linux__ | |
# include <sys/filio.h> | |
#endif | |
int main(int argc, char **argv) { | |
int use_ioctl; | |
int set; | |
int fd; | |
int i; | |
if (argv[1]) | |
fd = open(argv[1], O_RDONLY); | |
else | |
fd = STDERR_FILENO; | |
if (fd == -1) | |
return puts("open error"), EXIT_FAILURE; | |
use_ioctl = getenv("USE_IOCTL") && atoi(getenv("USE_IOCTL")); | |
set = 1; | |
for (i = 0; i < 10e6; i++) { | |
if (use_ioctl) { | |
if (ioctl(fd, FIONBIO, &set)) | |
abort(); | |
} | |
else { | |
if (fcntl(fd, F_SETFL, O_NONBLOCK)) | |
abort(); | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What's the verdict?