The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Fastest way decimal to segment in chars
Discuss Fastest way decimal to segment in chars in the C Programming forum on Dev Shed. Fastest way decimal to segment in chars C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

July 19th, 2006, 11:50 AM
|
|
Contributing User
|
|
Join Date: Jul 2006
Posts: 58

Time spent in forums: 15 h 40 m 2 sec
Reputation Power: 7
|
|
|
Fastest way decimal to segment in chars
Decimal can be representented as:
number=106;
sprintf(str,"%u",number);
str + 0=1;
str + 1=0;
str + 2=6;
any method faster than sprintf?
|

July 19th, 2006, 12:23 PM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
|
Assembly.
Or maybe if you rolled your own function.
BTW, if by
str + 0=1;
you really meant
str[0]='1';
, then you left out the most important part, the null-terminator:
str[3]='\0';
|

July 19th, 2006, 12:37 PM
|
 |
Contributed User
|
|
|
|
|
How did you determine that sprintf() is the cause of your performance problems?
|

July 19th, 2006, 10:11 PM
|
 |
Super User
|
|
|
|
Quote: | Originally Posted by level4
any method faster than sprintf? |
The answer to that question is "Yes." However, as salem suggested, the real question that needs to be answered is "Is it worth it to invest in optimizing this portion of my program?"
|

July 21st, 2006, 04:15 AM
|
 |
Google Relay Server
|
|
Join Date: Oct 2003
Location: Oh christ I don't even know any more.
|
|
Read through this thread, there are quite a few good ideas there.
__________________
OMG RAVER CHICKS!!
On a related note: C/C++ Programming Tutorials

"Science is based on reality staying the same, and Nature ignores what humans vote upon." -- Bill Beaty
"Three litres of sherry up the butt can only be described as astounding." -- Darwin Awards
|

July 21st, 2006, 10:57 AM
|
|
Contributing User
|
|
Join Date: May 2006
Posts: 48
  
Time spent in forums: 4 Days 15 h 29 m 4 sec
Reputation Power: 9
|
|
|
itoa()
|

July 21st, 2006, 11:27 AM
|
 |
Google Relay Server
|
|
Join Date: Oct 2003
Location: Oh christ I don't even know any more.
|
|
Quote: | Originally Posted by dwise1_aol Assembly. |
I feel like I've seen some interesting assembler algorithms for converting base 256 to base 10 (ones that aren't just divide with remainder repeatedly); but I don't think any of them used any "special" instructions like ror or anything, so you should be able to write C code that compiles to the exact same thing. Can't remember though.
Quote: | Originally Posted by Devorious itoa() |
That's probably the best bet since any function you rolled yourself would most likely use the same algorithm. But itoa() is deprecated in POSIX (for security reasons; same reason that gets() is unsafe), and so it's not really portable. MS compilers support it (with the underscore, _itoa), but it's not in the version of glibc I have on this machine, at least.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|