CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

sprintf vs strcpy advantages - which is better

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
benoitstjean



Joined: 30 Oct 2007
Posts: 542
Location: Ottawa, Ontario, Canada

View user's profile Send private message

sprintf vs strcpy advantages - which is better
PostPosted: Wed Dec 09, 2020 7:57 am     Reply with quote

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: 1899

View user's profile Send private message

PostPosted: Wed Dec 09, 2020 8:04 am     Reply with quote

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: 19195

View user's profile Send private message

PostPosted: Wed Dec 09, 2020 8:17 am     Reply with quote

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: 542
Location: Ottawa, Ontario, Canada

View user's profile Send private message

PostPosted: Wed Dec 09, 2020 8:18 am     Reply with quote

Ok, thanks guys!

Ben
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Wed Dec 09, 2020 8:23 am     Reply with quote

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: 542
Location: Ottawa, Ontario, Canada

View user's profile Send private message

PostPosted: Wed Dec 09, 2020 8:24 am     Reply with quote

Oh. Ok. I'm using the PIC24EP512.
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Wed Dec 09, 2020 8:48 am     Reply with quote

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: 542
Location: Ottawa, Ontario, Canada

View user's profile Send private message

PostPosted: Wed Dec 09, 2020 9:01 am     Reply with quote

You just opened the door to another question... what's PSV?
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Wed Dec 09, 2020 10:29 am     Reply with quote

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: 542
Location: Ottawa, Ontario, Canada

View user's profile Send private message

PostPosted: Wed Dec 09, 2020 1:29 pm     Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group