8

I recently found this site called codechef, where you can submit solutions to problems. I had submitted two answers for a question, one in C and the other in C++. Both codes are almost the same. But when the code I submitted in C was executed in 4.89s, the code I submitted in C++ was timed out (more than 8 seconds). How is this possible? Where does the time go?

The question was:

Input

The input begins with two positive integers n k (n, k<=107). The next n lines of input contain one positive integer ti, not greater than 10^9, each.

Output

Write a single integer to output, denoting how many integers ti are divisible by k.

Example

Input:
7 3
1
51
966369
7
9
999996
11

Output:
4

My code in C:

 #include<stdio.h>
   
 int main()  {
   
   int n,k,t;
   scanf("%d %d",&n,&k);
   int i,num=0;
   for(i=0;i<n;i++)  {
     scanf("%d",&t);
     if(t%k==0)  num++;
   }     
   
   printf("%d",num);
    
   return 0;
 }

My Code in C++:

 #include<iostream>
  
 using namespace std;
   
 int main()  {
  
   int n, k, t,num=0;
   cin>>n>>k;
   for(int i=0;i<n;i++)  {
     cin>>t;
     if(t%k==0)  num++;
   }
  
   cout<<num;
   return 0;
 } 
8
  • Sorry! That was 10^9. Copy paste error. :) Commented Mar 6, 2014 at 13:04
  • 1
    First of all try C++ with cstdio. Commented Mar 6, 2014 at 13:04
  • 1
    @any36 - So your timing included how fast someone can type in the input?? I see calls to scanf() in the C code and cin in the C++ code. Is this included in the timing test? Commented Mar 6, 2014 at 13:18
  • @PaulMcKenzie : The site tests the code with preset cases. So, I guess it doesn't take up time to type in the input. Commented Mar 6, 2014 at 13:20
  • @any36 - So you're not sure what they're doing with the I/O. If that's the case, then in my opinion, the timings are flawed. I could see if you're timing non I/O code, or even timing how long it takes to read and process a large file, but even that takes many test runs to coalesce around a firm result. Commented Mar 6, 2014 at 13:24

2 Answers 2

28

The code is not really the same even though they do the same thing

The c++ version uses cin and streams which are slower than scanf etc by default.

By default, cin/cout waste time synchronizing themselves with the C library’s stdio buffers, so that you can freely intermix calls to scanf/printf with operations on cin/cout. You can turn this off with std::ios_base::sync_with_stdio(false);

By doing this the time taken will more or less be similar I would expect

Sign up to request clarification or add additional context in comments.

11 Comments

+1 for pointing out sync_with_stdio instead of the classy iostreams are slow which is just wrong.
@arry36 For type safety reasons I stick with using streams, but other people I know do do it. If I were you I wouldnt however, C++ should be written like C++ imo ;)
The trade off is that C++ streams are no longer synchronized to the standard C streams after each input/output operation, so ideally you need to choose either/or and cant mix both
Oh! I get it now. All the streams in C++ are in synchronization all the time. That is why using streams take more time. Thank you so much! :)
|
1

I usually add these 3 lines in my code just after the main() for faster input output :

ios_base::sync_with_stdio(false);

cin.tie(NULL);

cout.tie(NULL);

So , Try this :

int main()  
{
   ios_base::sync_with_stdio(false);
   cin.tie(NULL);
   cout.tie(NULL);
   int n, k, t,num=0;
   cin>>n>>k;
   for(int i=0;i<n;i++)  {
     cin>>t;
     if(t%k==0)  num++;
   }

   cout<<num;
   return 0;
 } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.