Question:
Below program takes 9 minutes for printing the values. So, I want to reduce this time how it can possible?
suresh
2015-02-04 22:39:07 UTC
This is a C program in Linux

main()
{
static int j,a[1000000];
for(j=0;j<10000000;j++)
{
a[j]=rand();
printf("%d\t%d\n",j,a[j]);
}
}
Three answers:
Neerp
2015-02-05 07:25:20 UTC
Are you at the high school level, undergraduate college, or graduate college level? That makes a difference, because at the graduate level you are expected to know a few things that can optimize this that undergrads and high school students don't usually know.



From a computational tractability perspective, you probably can't. You are iterating from 0 to 999999. While this is being done in O(n) time, which in the over scheme of things is pretty good, you are still iterating one million times. If you have to iterate all one million times, you are stuck at O(n), and all you can do is try to optimize the code a bit.



One change would be to unroll the loop. This would make it run much faster, but would result in a couple of million lines of code and a larger executable. This is a common compiler optimization technique, and can make your code run much faster.



Another change would be to take advantage of the parallelism offered by multi core cpus, or the gpu. If you have a good gpu, you might be able to get 100 threads running in parallel, and this would make it run 100 times faster. If you know how to write multi threaded code that can be off loaded to the gpu, this would be a trivial task.



Getting rid of the printf will speed things up. There is a bit of a penalty to pay to format the results like this. You could write a simple output function and buffer the results in memory, and then dump it when you are done. That could make it twice as fast because of the printf overhead.



You could try to write your own rand() function, but I'm not sure you can improve it much.



You might try g++ if you are using a Microsoft compiler. You might find that makes it run much faster as well. If you are running on Windows, try it on Linux and see how much faster it runs - sometimes getting rid of the bloat of Windows can make a big difference. I moved some of my OpenGL code to Linux and I was amazed at how much faster it was.



But in the end you still have a O(n) time function, and unless you are a computer science grad student, you are going to be stuck with that.
Kookiemon
2015-02-05 04:40:21 UTC
Use setvbuf() to buffer STDOUT. It should cut your time by a third. Displaying text on screen has a cost to it which you've noticed. Why not send the output to a text file which takes almost no time at all and only costs storage space?





#include

#include



int main()

{

    char buf[2000];



    if(setvbuf(stdout, buf, _IOFBF, sizeof buf)) {

        perror("failed to change the buffer of stdout");

        return EXIT_FAILURE;

    }



    static int j,a[1000000];



    for(j=0;j<1000000;j++)

    {

        a[j]=rand();

        printf( "%d\t%d\n",j,a[j]);

    }



    fflush( stdout);



    return 0;

}
Greywolf
2015-02-05 01:44:14 UTC
Faster printer.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...