0

i write a basic c code to count up to 99999999999.

void main(){
long unsigned int i;
for(i=1;i<=99999999999;i++){
        printf("%lu",i);
        printf("\n");
        }

but it was taking too much time in the C compiler or in .exe file as i m using windows 7.

I want to get the number from 0 to 999999999 in seconds using C code and how much it takes in another languages like java. And how to get this using dos command and batch code. I am using core i7 2720qm.

indiv
  • 17,306
  • 6
  • 61
  • 82
  • 6
    writing to console is a relatively expensive operation so your current code is really just profiling `printf` performance – simonc Nov 13 '13 at 17:57
  • Note: That is 100-billion, not 1-billion. And if you compile with optimizations and remove the console output, don't be alarmed to see the execution time hit zero. (i.e. the compiler will throw out the entire, now-useless, loop). – WhozCraig Nov 13 '13 at 18:04
  • 1
    stdio is very very expensive compared to your counting. And if you just remove the printf's the compiler may well simply remove the loop, because it doesn't have any side effects. Writing performance tests is a tricky business. – Charlie Burns Nov 13 '13 at 18:05
  • Also note that if you're on a 32-bit platform, you probably ignored a compiler warning or three, and your loop may never terminate because a 32-bit `long unsigned int` will overflow and wrap back to zero somewhere north of 4 billion, never reaching 100 billion... (And by 32-bit platform, I don't mean the processor itself - 64-bit processors can be run as 32-bit processors when running older OSes, with all the limits implied by the 32-bit environment....) – twalberg Nov 13 '13 at 19:07

1 Answers1

1

You're never going to be able to get those numbers output in a realistic amount of time for comparison.

The problem is the process of streaming to the console is a more expensive operation, so this isn't a very effective method for performance comparison.

The best I can suggest is to measure the system time before and after and output the differences from before and after after the code is finished executing.

Plasmarob
  • 1,321
  • 12
  • 20
  • then how to get 0 to 999999999999 in a text file ? – sachinpanwar Nov 14 '13 at 15:17
  • 1
    Why on earth would you want to do that? It's not valuable information. I've handled prime numbers in the tens of billions - but still didn't need text files. What would you use the file for if you were to create it? If that was all you cared about - getting it out, see http://stackoverflow.com/questions/8650491/c-working-with-fopen-fclose-fputc-etc?rq=1 . You should be able to reproduce the numbers mathematically in code if you ever needed them again. – Plasmarob Nov 14 '13 at 16:20