How to print a 32-bit number as a base-10 number

S

Steve

Hi,

Assuming that "division" is a very costly operation (except by any
multiples of 2), whats the fastest way of printing a 32-bit number as a
base-10 unsigned integer.

Thanks
Steve
 
F

Flash Gordon

Assuming that "division" is a very costly operation (except by any
multiples of 2), whats the fastest way of printing a 32-bit number as
a base-10 unsigned integer.

puts("a 32-bit number as a base-10 unsigned integer.");

The C language does not define the relative speed of different
operations. printf and friends on your implementation might be highly
optimised assembler that you could not beet using C, or it might be
incredibly slow and easy to beet in C. It might be fastest to use
multiply and shift down (I know one processor where this is definitely
true) or multiple might be slow and you are best using a shift and
add/subtract algorithm.

In short, if you want to know what is fast for your implementation you
need to ask in a group dedicated to your platform.
 
G

Gregory Toomey

Steve said:
Hi,

Assuming that "division" is a very costly operation (except by any
multiples of 2),

Why do you say that? Have you looked at the time complexity of integer
division/multiplication?
whats the fastest way of printing a 32-bit number as a
base-10 unsigned integer.

Thanks
Steve

printf("%d",number);

gtoomey
 
W

Walter

Steve said:
Assuming that "division" is a very costly operation (except by any
multiples of 2), whats the fastest way of printing a 32-bit number as a
base-10 unsigned integer.

Printing a number goes through the I/O system, which is commonly orders of
magnitude slower than a few divides. You'll likely never notice the overhead
of the divides when printing numbers.

Assumptions about where a program is spending its time are usually wrong,
even when assumed by experts. A profiler will give you accurate information
that is often surprising.

Digital Mars C is a free download and comes with a profiler
www.digitalmars.com/ctg/trace.html.

-Walter
www.digitalmars.com free C, C++, D compilers
 
M

Martin Ambuhl

Steve said:
Hi,

Assuming that "division" is a very costly operation (except by any
multiples of 2), whats the fastest way of printing a 32-bit number as a
base-10 unsigned integer.

The fastest way (in terms of execution) is completely
implement-dependent. The fastest way to write it is with printf:

#include <stdio.h>

int main(void)
{
unsigned long x = 42; /* note that the guaranteed range of 'int'
is not sufficient. */
printf("%lu\n", x);
return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,835
Latest member
KetoRushACVBuy

Latest Threads

Top