How to see the implementation of a function?

2

From time to time I have some doubts about the efficiency of a code and I think if I see the implementation of a function that I'm using I can know how efficient it is or not.

Some examples of implementations that would remedy some doubts:

1) What is the implementation of <= or >= ? What are the differences from < , > and == ? Is the >= comparison to be two separate functions or one?

2) What is the implementation of getchar (), gets () and scanf ()? What's the difference between them?

3) What is the implementation of do..while() , while() and for() ? The last two seem to be the same thing but in a different way to put the parameters. Would it be the same implementation? How efficient is it to use for() instead of while() and vice versa?

I have already read some documentation of the C language and compiler, which I use, GCC, but I would really like to see these implementations, or something very close that could clarify these doubts.     

asked by anonymous 25.09.2014 / 23:49

3 answers

4

Editing here, complementing the original answer below: The implementation of these functions and operators is a very complex thing, and if you are a beginner I recommend not stressing it now. Assume that the implementations of low-level languages are as efficient as possible. Wanting to know how these things work is one of the hallmarks of a good programmer, but until you master language and compiler building techniques, it is more important to know the algorithms and correct use of these functions than your actual implementation.

The implementation of language functions is separate from the language itself;)

Greater or smaller comparisons, type a > b , can be done differently by different processors. Your phone's ARM can do it one way, while the i3 / 5/7 of your PC can do it another. But a line of code like:

if (a < 3) { /* .. snip .. */ }

... It should work on both.

So what's the secret? Each processor has its own assembly language. C, C ++ and other low-level languages are a way for you to express commands to the machine, but at the end of the day the compiler takes what you have written and translates it into the machine language of the processor it is compiling for.

To make it even funer, two different compilers can generate different assembly code for the same input source code.

And to get even more funny - as the compiler is written to run on an operating system, the compiler libraries themselves - which contain these methods - may vary from one operating system to another.

If you want to see the implementation of these functions, the way I suggest is to read the source code of C compilers and libraries.

Here's the source of the GNU C libraries: link

There you will find, among others, scanf and printf , as implemented for Linux. Note that the part where it handles the target architecture still gets below that, so good digging;)

    
26.09.2014 / 00:27
4
  

What is the difference between getchar() , gets() and scanf() ?

getchar reads one character at a time, gets reads one whole line at a time, and scanf is a generic function that does different things according to the format string you use. These functions do very different things and it is better to choose the most appropriate one instead of worrying about micro optimizations (the cost of doing input and output operations and calls to the operating system is probably much greater than that.)

For the rest of your questions, I suggest you learn how to read the executable files generated by your compiler. In the case of gcc you can use the -S flag to get an assembly language version of the executable generated from your program:

gcc -S meuprograma.c -o meuprograma.S

The compiler has enough freedom to change the structure of your program, as long as the result is the same as the original. I think you'll be amazed at the result in some cases :) Just a tip: To simplify things, just write a function with the code of your interest - leave the input and output aside.

Note that this all depends on the compiler you use, the level of optimization (-O0, -O1, -O2) and your processor architecture (x86, x86-64, ARM, etc.)

  

1) What is the implementation of < = or > =? What are the differences from & lt ;, > and ==? The comparison > = would be two separate functions or one?

Do not worry about it. Your processor will probably spend the same time for any of these comparisons, and even if it were different, your compiler would probably account for doing the micro-optimizations itself (for example, if(a < b){ XXX }else{ YYY } is the same as if(a >= b){ YYY }else{ XXX } ) .

  

3) What is the implementation of do..while (), while () and for ()? The last two seem to be the same thing but in a different way to put the parameters. Would it be the same implementation? How efficient is it to use for () instead of while () and vice versa?

It's all equally efficient. your compiler will convert all structured control structures (if, while, for, etc) into a lower-level execution flow graph, and in the end will spit out a soup of% unstructured%.

Just a warning for your adventures: It's hard to guess how long the computer will take to do each operation and it's even harder to predict which part of your program is the most sensitive in terms of performance (no use nothing double the speed of a stretch responsible for 1% of the total run time). Always use a profiler to take empirical measures of the time spent and remember that the CPU does not perform all operations at the same speed (for example, nowadays the speed of memory access tends to be a bottleneck much larger than the number of operations performed by the CPU)

    
26.09.2014 / 03:24
0

Answering the 1st question:

There is no difference in the implementation question, possibly the comparator> = or <="perform the function" of 2 comparators because it first checks whether the value in question is equal and then checks whether the value is higher or lower .

Answering the 2nd question:

The GETCHAR () function reads a character and returns an integer that is: - the character code, or - the value -1 corresponding to the end of the file.

The GETS function of the standard C (stdio) library can cause a major problem for the programmer using it: since this function does not limit the number of characters to be read from the stdin, there may be memory, or even worse, malicious code injection into the program.

The solution is to use FGETS, which limits the read buffer.

The function used to read "generic" values (receives any primitive type) is the SCANF function.

Answering the 3rd question:

WHILE X FOR:

FOR executes a limited, fixed number of steps, whereas WHILE can do this indefinitely.

For example, if I make a loop to add 1 to a variable, already declared, n times, the loop will always run n times. The initial and final result will always be the same.

While the while is used when the user is going to set the initial value of this variable, the control is only for the maximum value.

Being clearer (Exemplifying):

The program declares x at first as 1. Then it executes the FOR (loop) 10 times, and in each of them the value will be incremented by 1. The final result will always be 11.

If it is a WHILE loop, written so that this variable x reaches 10 (while x

26.09.2014 / 06:28