Pages: [1]   Go Down
Author Topic: efficacy question  (Read 934 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Sr. Member
****
Karma: 1
Posts: 415
MEGA_Stick_II is here
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Which is more efficient, or is it preference?
Code:
if(PreLevel[i] != Percent[i]) // only update on change
     {
     Serial.println("progress_value "+String(i)+" "+String(Percent[i]));
     PreLevel[i] = Percent[i];
     }

Or
Code:
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
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
Netherlands
Online Online
Shannon Member
*****
Karma: 211
Posts: 13479
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


The simplest way is just measure it in a sketch

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

Logged

Rob Tillaart

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

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 72
Posts: 7171
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

For better quality program, try sprintf:

Code:
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/
Logged


0
Offline Offline
Sr. Member
****
Karma: 1
Posts: 415
MEGA_Stick_II is here
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


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.



Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

0
Offline Offline
Sr. Member
****
Karma: 1
Posts: 415
MEGA_Stick_II is here
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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/
Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 72
Posts: 7171
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged


Global Moderator
Netherlands
Online Online
Shannon Member
*****
Karma: 211
Posts: 13479
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 -


Logged

Rob Tillaart

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

0
Offline Offline
Sr. Member
****
Karma: 1
Posts: 415
MEGA_Stick_II is here
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.


Logged

Global Moderator
Netherlands
Online Online
Shannon Member
*****
Karma: 211
Posts: 13479
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

@liudr
Code:
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() ...)
Logged

Rob Tillaart

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

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 72
Posts: 7171
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I was in a hurry smiley-red
Logged


Pages: [1]   Go Up
Jump to: