How to make this code time efficient?

// Factorial.cpp
// 3
// Created by Chintan Shah on 17/08/13.
// Copyright © 2013 Chintan Shah. All rights reserved.

#include <iostream>
using namespace std;

int main()
    int t;
    double n;
    int c=5;
    long a[t];
    for(int i=0;i<t;i++)
    while (c<=n)
    for(int i=0;i<t;i++)
    return 0;

This is factorial program where we have to calculate number of trailing zeros

It took 1.6s to run and there are submissions with 0.02 sec (though they are really advanced and I didnt get anything)

Actually, i interpret you program wrong. You need faster i/o as said by @kunal361. Use scanf()/printf().

the main cause of slow program is slow I/O…

my code with cin/cout took 1.7+ secs…LINK!!!

and the one with fast I/O took 0.09 secs…LINK!!!

Hello @chintanshah,

One major improvement and relatively simple one you can use on your program is to replace the I/O methods cin/cout, with scanf()/printf().

Generally, the former ones are a lot of times faster than cin and cout and actually it is an encouraged practice to do so!!

Also, putting the code which “does the actual job” in an auxiliary function and inlining it, might also give you a relatively small performance boost :smiley:

About the solutions you don’t understand, possibly they are using very fast I/O methods, which are based on reading the numbers from the standard input as a stream of characters and convert them to a number afterwards :slight_smile:

Best regards,


How can putting some code in another function and then inlining it give performance boost? Inlining the code itself (without any functions) should be faster compared to any use of functions, right?

Well yes, using no functions at all and only using fast I/O should be faster compared with inlining any functions, absolutely!

I guess what I meant to say is that, on the case where there are auxiliary functions, inlining them might help :wink:

1 Like

Oh, yes! An inlined function is marginally better against the corresponding non-inlined function.

The quick idea I inferred from the answer was otherwise! :stuck_out_tongue: