0
Offline
Sr. Member
Karma: 1
Posts: 405
MEGA_Stick_II is here
|
 |
« on: April 29, 2012, 02:53:40 pm » |
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
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Offline
Brattain Member
Karma: 315
Posts: 35519
Seattle, WA USA
|
 |
« Reply #1 on: April 29, 2012, 03:05:25 pm » |
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
|
|
|
|
|
Netherlands
Offline
Tesla Member
Karma: 90
Posts: 9403
In theory there is no difference between theory and practice, however in practice there are many...
|
 |
« Reply #2 on: April 30, 2012, 12:51:23 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Central MN, USA
Offline
Faraday Member
Karma: 35
Posts: 5934
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
|
 |
« Reply #3 on: April 30, 2012, 01:04:56 pm » |
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/
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Sr. Member
Karma: 1
Posts: 405
MEGA_Stick_II is here
|
 |
« Reply #4 on: April 30, 2012, 05:04:14 pm » |
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
Brattain Member
Karma: 315
Posts: 35519
Seattle, WA USA
|
 |
« Reply #5 on: April 30, 2012, 05:33:44 pm » |
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
Sr. Member
Karma: 1
Posts: 405
MEGA_Stick_II is here
|
 |
« Reply #6 on: April 30, 2012, 08:51:50 pm » |
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
Melbourne, Australia
Offline
Shannon Member
Karma: 219
Posts: 13896
Lua rocks!
|
 |
« Reply #7 on: April 30, 2012, 09:35:00 pm » |
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/
|
|
|
|
|
Logged
|
|
|
|
|
Central MN, USA
Offline
Faraday Member
Karma: 35
Posts: 5934
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
|
 |
« Reply #8 on: April 30, 2012, 11:31:40 pm » |
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
|
|
|
|
|
Netherlands
Offline
Tesla Member
Karma: 90
Posts: 9403
In theory there is no difference between theory and practice, however in practice there are many...
|
 |
« Reply #9 on: May 01, 2012, 12:01:21 am » |
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
|
|
|
|
|
0
Offline
Sr. Member
Karma: 1
Posts: 405
MEGA_Stick_II is here
|
 |
« Reply #10 on: May 01, 2012, 06:25:10 am » |
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
|
|
|
|
|
Netherlands
Offline
Tesla Member
Karma: 90
Posts: 9403
In theory there is no difference between theory and practice, however in practice there are many...
|
 |
« Reply #11 on: May 01, 2012, 12:23:56 pm » |
@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() ...)
|
|
|
|
|
Logged
|
|
|
|
|
Central MN, USA
Offline
Faraday Member
Karma: 35
Posts: 5934
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
|
 |
« Reply #12 on: May 01, 2012, 02:15:15 pm » |
I was in a hurry 
|
|
|
|
|
Logged
|
|
|
|
|
|