Performance gain by avoiding arguments check is very small (less then 1%).
Amit
amitchoudhary0523@gmail.com
Mon Sep 29 15:42:54 GMT 2025
Hi,
I have seen the argument that glibc/POSIX don't want to validate the
arguments of a function because of performance reasons.
So, I decided to do an experiment to measure how much performance
improvement can be gained by avoiding validation of arguments.
The code is below in the mail. I tested the code on my linux system. You
guys can also run the code on your linux systems and see it for yourself.
=========================================
I found out that the performance gain is only 0.775%.
=========================================
""""So, now I think that glibc/POSIX shouldn't say that arguments
validation is not done so that performance can be improved because the
performance improvement is very very very very less.""""
""""It is so less that it is better to validate the arguments to avoid
crashes, etc.""""
Below is the code and the result (don't look at the correctness of the
code, look at the NULL check):
----------------------
measure_time.c
----------------------
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#define MAX_LENGTH_ALLOWED 1024
int get_strlen(const char *str)
{
int i = 0;
if (!str) {
return -1;
}
for (i = 0; i <= MAX_LENGTH_ALLOWED; i++) {
if (str[i] == 0) {
return i;
}
}
return MAX_LENGTH_ALLOWED;
}
int main(void)
{
int len = get_strlen("1234");
exit(EXIT_SUCCESS);
}
------------------------------
run_measure_time.sh
------------------------------
#!/bin/bash
count=0
while true
do
count=$((count+1))
if [ "$count" -eq 100000 ]; then
exit
fi
./a.out
done
======
Result
======
----------------------------
With NULL checking
----------------------------
real 0m49.636s
user 0m35.720s
sys 0m17.183s
Total: user + sys = 52.903 seconds
--------------------------------
Without NULL checking
--------------------------------
real 0m49.323s
user 0m35.261s
sys 0m17.232s
Total: user + sys = 52.493 seconds
Performance improvement without NULL checking: 0.775%
----------------------------------------------------------------------------------------------------------------------------------------------
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://sourceware.org/pipermail/libc-alpha/attachments/20250929/72a4fc31/attachment.htm>
More information about the Libc-alpha
mailing list