Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In C is it faster to use the standard library or write your own function?

For example, in <ctype.h> there are functions like isalpha().

I want to know if writing an isalpha function on my own is faster than calling isalpha?


Thanks for all your instant replies! just want to make clearer to my question:

so even for the isalpha function? because you can simply pass a character and check if the character is between 'a' and 'z' || 'A' and 'Z'?

another question: when you include a std library like ctype.h and just call one function like isalpha, will the file(I mean all lines of code) be loaded? My concern is that the big-size will make program slower

like image 367
draw Avatar asked Jul 13 '10 01:07

draw


People also ask

Which code runs faster with function or without function?

Functions CAN make code faster by coding logic once instead of repeating several times and thus reducing code size and resulting in better CPU cache usage. Functions CAN make code slower by copying parameters and hiding info from the optimization.

Is the C standard library written in C?

In a typical case, the C standard library is written primarily in C, and the C++ standard library primarily in C++. To give some concrete numbers, Microsoft's standard library has ~1050 C and C++ files, and 37 assembly language files.

What happens if standard library is not present in C?

But in pure C, with no extensions, and the standard library functions removed, you basically can't do anything other than read the command line arguments, do some work, and return a status code from main .


2 Answers

Unless you have specific reason to do so (e.g., you have a specific requirement not to use the standard library or you've profiled a very specific use case where you can write a function that performs better), you should always prefer to use a standard library function where one exists rather than writing your own function.

The standard library functions are heavily optimized and very well tested. In addition, the standard library that ships with your compiler can take advantage of compiler intrinsics and other low-level details that you can't portably use in your own code.

like image 197
James McNellis Avatar answered Sep 20 '22 19:09

James McNellis


isalpha does not merely check if its argument is in the ranges A-Z, a-z. Quoting the C standard (§7.4.1.2):

The isalpha function tests for any character for which isupper or islower is true, or any character that is one of a locale-specific set of alphabetic characters for which none of iscntrl, isdigit, ispunct, or isspace is true.

In all likelihood you can write a more limited version (as you suggest) that is faster for the subset of cases that it handles, but it won't be the isalpha function. Library routines exist not only to be efficient, but to be complete and correct. Efficiency actually turns out to be the easy part; getting all the edge cases right is where the hard work comes in.


Note also, if you're going to write an optimized version that targets English/ASCII, you can do it rather more efficiently then what you suggested, either with the lookup table that someone else suggested, or my personal preference (edited to fix an error caught by R..)

int isalpha(int c) {
    return ((unsigned int)(c | 32) - 97) < 26U;
}
like image 35
Stephen Canon Avatar answered Sep 19 '22 19:09

Stephen Canon