Evaluating Coding performance

Hi all,

how could I evaluate how efficient is my code?I mean, generally I prefer to write code in a way that is

simple to read. i.e. I used to use function() like this Arduino example:

void loop() {
  int i = 2;
  int j = 3;
  int k;

  k = myMultiplyFunction(i, j); // k now contains 6
  Serial.println(k);
  delay(500);
}

int myMultiplyFunction(int x, int y){
  int result;
  result = x * y;
  return result;
}

instead of doing this:

void loop()
{int i = 2;
  int j = 3;
  int k;

  k = i*j;
  Serial.println(k);
  delay(500);
}

This is a basic example of course;

in term of performances, are the 2 examples equals? And what if I use multiple functions?

Hope my question is clear enough :slight_smile:

Thanks

how could I evaluate how efficient is my code?

First, you define what "efficient" means.

With such a basic example, the compiler will see what you're trying to do and it will "inline" the function. Both versions will result in identical machine code.

My definition of efficiency is "programmer efficiency". If the project works and does the job it's designed to do, then the programmer is much more efficient working on the next project instead of polishing (and possibly breaking) the already-working project. The computers serve us, not the other way around. They don't get bored asking "Did you push that button yet?" 10,000 times per second.

hi AWOL,

don't know actually :o , I hoped someone could explain better my question :slight_smile:

maybe the Instructions per second ?

I mean, in the first example k is given by myMultiplyFunction(i, j); so the "code has to jump" on the

myMultiplyFunction() part and then print the result, while in the second example it seems to be faster.

Is it more clear what am I asking ? ( newbie question :slight_smile: )

thanks again

It helps to write code with functions. It makes a clear logical organization. You can think about what the code is doing more easily.

The compiler doesn't compile into functions. It does whatever the heck it wants to reorganize your code into something which is logically equivalent (it does the same stuff) but totally unrecognisable to you.

If you try to test this, like by recording the millis() when you start and finish, then you will get a big surprise: the multiplication will take zero time to run on the Arduino. But it's equivalent - the result printed is identical to what you programmed. The compiler will do this multiplication when it compiles, not when it runs.

Creating, and calling, functions is far better then inlining the code. The compiler will do the inlining when it makes sense. But, it is NOT the output of the compilation step that you are trying to debug. It is the input. Make the input as logical and well-organized as you can, and let the compiler worry about producing efficient code.

Hi MorganS ,

thank you for your reply.

this:

With such a basic example, the compiler will see what you're trying to do and it will "inline" the function. Both versions will result in identical machine code.

and this:

If you try to test this, like by recording the millis() when you start and finish, then you will get a big surprise: the multiplication will take zero time to run on the Arduino. But it's equivalent - the result printed is identical to what you programmed. The compiler will do this multiplication when it compiles, not when it runs.

what I was looking for :slight_smile:

thanks again

PaulS:
Creating, and calling, functions is far better then inlining the code. The compiler will do the inlining when it makes sense. But, it is NOT the output of the compilation step that you are trying to debug. It is the input. Make the input as logical and well-organized as you can, and let the compiler worry about producing efficient code.

And you can declare functions inline:

inline int myMultiplyFunction (int a, int b)
{
  return a * b ;
}

Which is a strong hint for the compiler to inline. Of course the compiler might decide if the function is large
that inlining it is a poor choice and ignore you.

It is good practise to code as if other people will read your code one day. This means understanding and using the common idioms that C programmers use.

As for this example, I dislike it. If I look at a line of code that say k = i * j, I know exactly what it will do. If it's a function call, I have to scroll through the code and find the function. After all, "multiply" can mean quite a few different things. But for more complex blocks of work, it is good practise to separate out logical components.

The first and most important (often the only) documentation in code is the names that you give to your identifiers - your functions and variables.

Thanks all for your reply :slight_smile:

So far I have another question linked to the previous: and what about programming mcu in C or in C++

(i.e. programming an Arduino Uno in C or using the Arduino language and libraries ).

Some "old school engineers" told me that using Arduino language is not as good as programming in pure C

in term of perfomances in general(or that Arduino is useful only for playing... :zipper_mouth_face: )

As I told before, i prefer writing code in a way that is as most human readable as possibile. This is an example found here: Programming Arduino Uno in pure C | Freedom Embedded

#include <avr/io.h>
#include <util/delay.h>
 
#define BLINK_DELAY_MS 1000
 
int main (void)
{
 /* set pin 5 of PORTB for output*/
 DDRB |= _BV(DDB5);
 
 while(1) {
  /* set pin 5 high to turn led on */
  PORTB |= _BV(PORTB5);
  _delay_ms(BLINK_DELAY_MS);
 
  /* set pin 5 low to turn led off */
  PORTB &= ~_BV(PORTB5);
  _delay_ms(BLINK_DELAY_MS);
 }
}

This instead of the Arduino "Blink example" uses less RAM and Flash on an Arduino UNO, but I prefer to use Arduino language because it's easier to understand ( especially for a newbie ).

So why should I use pure C instead of a Arduino language which is a lot more understandable ?(of course if I don't have any issues due to flash size or SRAM)

Using C++ libraries/code will affects only the size of the sketch and the SRAM?

Thanks

Your old school engineer buddy does not know what he is talking about... it's pure C and just up to the programmer to use or not the IDE and libraries...

it's pure C

The HardwareSerial class would beg to differ. So would a lot of other classes.

DCloud:
Some "old school engineers" told me that using Arduino language is not as good as programming in pure C

Send those engineers over here, and we will politely explain to them that there is no "Arduino Language". There is an Arduino platform, there are Arduino libraries, there's an arduino main() loop that most programmers never see, but the language is simply C++.

Send those engineers over here, and we will politely explain to them

Speak for yourself. I don't intend to be polite to people that spout nonsense.

Speak for yourself. I don't intend to be polite to people that spout nonsense.

hahahah :smiley: :smiley: :smiley: :smiley: :smiley:

So using C++ libraries/code instead of using C will affects only the size of the sketch and the SRAM?

So using C++ libraries/code instead of using C will affects only the size of the sketch and the SRAM?

No. If a C++ library does something, such as what the HardwareSerial class does, changing the functionality from a class instance performing a task to a function performing the same functionality does not necessarily result in less code or less SRAM usage.

The Arduino does NOT execute C++ code. The compiler converts the C++ code to machine instructions, the linker combines all the machine instruction chunks together, adjusting offsets as needed, and avrdude tells the Arduino "Make it so". The Arduino runs the hex code, and doesn't know, or care, that it was produced from C, C++, Basic, or Cobol.

int myMultiplyFunction(int x, int y){
  int result;
  result = x * y;
  return result;
}

How do you think that this is more readable to use than just the multiplication operator (*)? This is just dumb. Not to mention it's two lines longer than needed. Just return x * y; and be done with it.

If I see a function call, I'm going to assume there's some reason for it. The only reason to have a function call like this that just wraps an operator is to use it as a callback. Indeed, the STL has wrapper functions for all of the operators for exactly this reason. Directly using the wrapper function is just weird. What benefit do you think you are getting my hiding the plain functionality behind a wrapper like this? Could you someday change the multiple function to perform a different operation, like subtraction?

Could you someday change the multiple function to perform a different operation, like subtraction?

I saw it as a simple example of using a function vs. inlining the code. I think you are reading too much into the example.

PaulS:
I saw it as a simple example of using a function vs. inlining the code. I think you are reading too much into the example.

I don't assume anything about the skill levels of people I am unfamiliar with. It's perfectly reasonable to think that an inexperienced person would take the advice of "put things into functions" too far because they don't understand what the guideline is for.

In a world where people will #define TWO 2 because "don't use magic numbers!", anything goes.

PaulS:
The HardwareSerial class would beg to differ. So would a lot of other classes.

Sure was just reacting to the C part of the comment