efficacy question

Which is more efficient, or is it preference?

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

Or

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

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.

The simplest way is just measure it in a sketch

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.

For better quality program, try sprintf:

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/

liudr: 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.

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.

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.

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

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/

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.

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 -

liudr:

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.

@liudr

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() ...)

I was in a hurry :blush: