Skip to content

Instantly share code, notes, and snippets.

@bnoordhuis
Created August 21, 2012 21:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bnoordhuis/3419436 to your computer and use it in GitHub Desktop.
Save bnoordhuis/3419436 to your computer and use it in GitHub Desktop.
test ioctl() vs fcntl() syscall speed
// 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;
}
// 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;
}
@jamesamcl
Copy link

What's the verdict?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment