Go Down

Topic: efficacy question (Read 1 time) previous topic - next topic

Jassper

Which is more efficient, or is it preference?
Code: [Select]

if(PreLevel[i] != Percent[i]) // only update on change
     {
     Serial.println("progress_value "+String(i)+" "+String(Percent[i]));
     PreLevel[i] = Percent[i];
     }


Or
Code: [Select]

if(PreLevel[i] != Percent[i]) // only update on change
     {
     Serial.print("progress_value ");
     Serial.print(String(i));
     Serial.print(" ");
     Serial.println(String(Percent[i]));
     PreLevel[i] = Percent[i];
     }

note i is from a for/next loop

Just wondering if there is any benefit to either way

Thanks

PaulS

Neither. From an efficiency point of view, having the String class consume resources doing what the Serial.print() method already knows how to do is a waste.

robtillaart


The simplest way is just measure it in a sketch

Code: [Select]

uint32_t start = millis();
for (int i=0; i<1000; i++)
{
  your code;
}
uint32_t stop = millis();

Serial.print("duration: ");
Serial.println(stop-start, DEC);


The question is seldom which is more efficient, but is the code efficient enough to meet the requirements. (and yes PaulS is right about the String class)

Guess second one is faster.

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

liudr

For better quality program, try sprintf:

Code: [Select]
char output[]="Progress value %d %d";
char buffer[25]; // Give enough space here.
sprintf(buffer,i,Percent[i]);
Serial.println(buffer);


You don't assemble a message as you print it out, you assemble the message, then print it out.

Tutorial:

http://liudr.wordpress.com/2012/01/16/sprintf/

Jassper



You don't assemble a message as you print it out, you assemble the message, then print it out.



That makes a lot of sense and thanks for the link.
I don't need my code to look like a pro did it, but I would like to try and keep it clean and tidy.

So wouldn't this be just as good, or I take it that sprintf as you showed is faster?

string S = "this"+"that";
Serial.print(S):

Thanks guys for the tips.




PaulS

Quote
So wouldn't this be just as good,

If you'd tried compiling that, you'd know.

Create two sketches. One should use sprintf and a static array. The other should use the String class. The output should be the same. See which one uses more flash memory.

Jassper

OK, I have a lot of testing to do I see.
Is the sprintf new to 1.0? I don't remember reading about it with Arduino before that.

Nick Gammon

If you use the Streaming library you get the nice look of concatenation with no extra overhead (and no extra buffers):

Code: [Select]
if(PreLevel[i] != Percent[i]) // only update on change
     {
     Serial << "progress_value " << i << " " << Percent [i] << endl;
     PreLevel[i] = Percent[i];
     }


http://arduiniana.org/libraries/streaming/
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

liudr


OK, I have a lot of testing to do I see.
Is the sprintf new to 1.0? I don't remember reading about it with Arduino before that.



sprintf is a C function, as old as C is.

robtillaart

Quote
Is the sprintf new to 1.0? I don't remember reading about it with Arduino before that.


these libs are all usable from arduino code - http://www.gnu.org/savannah-checkouts/non-gnu/avr-libc/user-manual/modules.html -


Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Jassper



OK, I have a lot of testing to do I see.
Is the sprintf new to 1.0? I don't remember reading about it with Arduino before that.



sprintf is a C function, as old as C is.

Yah I know that, I just don't remember seeing it mentioned in the Arduino reference. Now it's there. Guess I just didn't pay much attention, but I haven't messed with strings much until recently.



robtillaart

@liudr
Code: [Select]
har output[]="Progress value %d %d";
char buffer[25]; // Give enough space here.
sprintf(buffer,i,Percent[i]);
Serial.println(buffer);


I think you did not test this code snippet. (hint output is missing in sprintf() ...)
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

liudr


Go Up