<div dir="ltr">To be sure I did one more experiment. This time I used qsort() and I validated 4 arguments.<div><br></div><div><div>=========================================</div><div>I found out that the performance gain is only 2.61%. In my last experiment where I was checking only 1 argument the performance improvement was 0.775%. So, per argument you get a gain of between 0.65% to 0.775%.</div><div><br></div><div>I don't remember which glibc/POSIX function has the maximum number of arguments but assuming that the max number is 5, then you will get only</div><div>3.25% - 3.875% performance gain.</div><div><br></div><div>Does this small gain justify not validating the arguments? In my opinion, it doesn't. But definitely, you guys can think otherwise.</div><div>=========================================</div></div><div><br></div><div>Below is the code and the result:</div><div><br></div><div>-----------------------</div><div>measure_qsort.c</div><div>------------------------</div><div><br></div><div>#include <stdio.h><br>#include <stdlib.h><br>#include <limits.h><br><br>#define MAX_NMEMB_ALLOWED 100*1024*1024 // 100 MB<br><br>#define MAX_MEMBER_SIZE_ALLOWED 1024<br><br>typedef int (*COMP_FUNC)(const void *elem1, const void *elem2);<br><br>int comp_func(const void *elem1, const void *elem2)<br>{<br><br>    if ((elem1 == NULL) && (elem2 == NULL)) {<br>        return 0;<br>    } else if (elem1 == NULL) {<br>        return -1;<br>    } else if (elem2 == NULL) {<br>        return 1;<br>    }<br><br>    return ((*((int *)(elem1))) - (*((int *)(elem2))));<br>}<br><br>void do_qsort(void *arr, size_t nmemb, size_t size, COMP_FUNC comp_func)<br>{<br><br>#if 0<br>    if (!arr) {<br>        return;<br>    }<br><br>    if ((nmemb <= 0) || (nmemb > MAX_MEMBER_SIZE_ALLOWED)) {<br>        return;<br>    }<br><br>    if ((size <= 0) || (size > MAX_MEMBER_SIZE_ALLOWED)) {<br>        return;<br>    }<br><br>    if (!comp_func) {<br>        return;<br>    }<br>#endif<br><br>    qsort(arr, nmemb, size, comp_func);<br><br>}<br><br>int main(void)<br>{<br><br>    int arr[4] = {20, 1, 50, 3};<br>    int i = 0;<br><br>    do_qsort(arr, 4, 4, comp_func);<br><br>    exit(EXIT_SUCCESS);<br><br>}<br></div><div><br></div><div>-------------------------------</div><div>run_measure_qsort.sh</div><div>-------------------------------</div><div><br></div><div>#!/bin/bash<br><br>count=0<br><br>while true<br>do<br>    if [ "$count" -eq 100000 ]; then<br>        exit<br>    fi<br>    ./a.out<br>    count=$((count+1)) <br>done<br></div><div><br></div><div>======</div><div>Result</div><div>======</div><div><br></div><div>------------------------------------<br>With arguments validation<br>------------------------------------<br><br>real      0m50.952s<br>user 0m36.610s<br>sys  0m17.766s<br><br>Total: user + sys = 54.376 seconds<br><br>----------------------------------------<br>Without arguments validation<br>----------------------------------------<br><br>real     0m49.701s<br>user 0m35.723s<br>sys  0m17.235s<br><br>Total: user + sys = 52.958 seconds<br><br>Performance improvement without arguments validation: 2.61%</div><div><br></div><div>--------------------------------------------------------------------------------------------------------------</div><div><br></div></div>