View previous topic :: View next topic |
Author |
Message |
benoitstjean
Joined: 30 Oct 2007 Posts: 566 Location: Ottawa, Ontario, Canada
|
sprintf vs strcpy advantages - which is better |
Posted: Wed Dec 09, 2020 7:57 am |
|
|
Compiler: 5.026
Just as a general question, which function is better / most effective (speed and code-usage) between strcpy and sprintf when copying text to another string?
Code: |
sprintf( MyString, "abcdef" );
|
AnotherString: abcdef
Code: |
sprintf( MyString, "%s", AnotherString );
strcpy( MyString, "abcdef" );
|
AnotherString: abcdef
Code: |
strcpy( MyString, AnotherString );
|
If I want to save on code space, for this type of string copying, is there anyone that is better than the other?
Thanks.
Ben |
|
|
newguy
Joined: 24 Jun 2004 Posts: 1907
|
|
Posted: Wed Dec 09, 2020 8:04 am |
|
|
Cut a test program which uses all 3 options then look at the .lst file to see which is more efficient. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19510
|
|
Posted: Wed Dec 09, 2020 8:17 am |
|
|
Generally, the strcpy from RAM to RAM will be the fastest.
So
strcpy( MyString, AnotherString );
However this brings the 'overhead' of needing a second area of RAM.
This is inherent, because of the complexities of accessing the ROM on the
PIC.
The next fastest will be:
strcpy( MyString, "abcdef" );
You don't show this, but it is legal to use a constant string in strcpy
like this, and the compiler optimises this the best of all.
Then:
sprintf( MyString, "abcdef" );
Finally:
sprintf( MyString, "%s", AnotherString );
Generally, expect perhaps about 2:1 difference between the fastest and
slowest, withe the biggest jump between the RAM to RAM one, and the
others.
Using strcpy, with the constant as shown will be perhaps 20% faster
than the other methods. |
|
|
benoitstjean
Joined: 30 Oct 2007 Posts: 566 Location: Ottawa, Ontario, Canada
|
|
Posted: Wed Dec 09, 2020 8:18 am |
|
|
Ok, thanks guys!
Ben |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19510
|
|
Posted: Wed Dec 09, 2020 8:23 am |
|
|
Worth also perhaps adding that the answer does depend on what chip
family is involved. My answer was based on a PIC18. |
|
|
benoitstjean
Joined: 30 Oct 2007 Posts: 566 Location: Ottawa, Ontario, Canada
|
|
Posted: Wed Dec 09, 2020 8:24 am |
|
|
Oh. Ok. I'm using the PIC24EP512. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19510
|
|
Posted: Wed Dec 09, 2020 8:48 am |
|
|
Ah, well on the PIC24 you can enable PSV as an option. Doing so massively
improves the speed of ROM accesses.
Doing this the order becomes:
strcpy( MyString, AnotherString );
strcpy( MyString, "abcdef" );
sprintf( MyString, "%s", AnotherString );
sprintf( MyString, "abcdef" );
But are also about 3* faster at the same instruction rate. The table access
operations make this type of movement much better. |
|
|
benoitstjean
Joined: 30 Oct 2007 Posts: 566 Location: Ottawa, Ontario, Canada
|
|
Posted: Wed Dec 09, 2020 9:01 am |
|
|
You just opened the door to another question... what's PSV? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19510
|
|
Posted: Wed Dec 09, 2020 10:29 am |
|
|
Prpgram Space Visibility.
Normally PIC's can't access their program memkory except by quite
complex instructions. On the PIC24/33's though you can setup PSV,
With this data has to be stored in the program space 2 bytes to the word,
but can then be accessed through a 'window' in the RAM space. Allows
you to use pointers to the stored values, and access them through the
normal pointer registers. As if the values are in RAM!...
You just have to specify #DEVICE PSV=TRUE and the compiler will set
this up. |
|
|
benoitstjean
Joined: 30 Oct 2007 Posts: 566 Location: Ottawa, Ontario, Canada
|
|
Posted: Wed Dec 09, 2020 1:29 pm |
|
|
I just tried it and it says I don't have enough RAM for all variables. When the code compiles, without this directive, the RAM usage is 58% and ROM is 92%.
I'll leave it at that for now I guess... Good to know though.
Thanks again,
Ben |
|
|
|