Hello everybody , today I'll be testing speed of input/output on Codeforces. My objective is to provide assistance on determining input method for problems (You wouldn't want to get TLE because of I/O right?) and calculate the actual time your algorithm need to work in.
(I am currently Updating this post, if you wanted your language and method to be listed here , please contact me and your name will be largely on this post Xp)!!
Since the post has become very long now , I suggest searching for specific language or you might get dizzy and I cannot deliver you medicine on time :(
Languages tested: C/C++ , JAVA
How do I test?
I made two problems in polygon , then I create mashup contest from them. So I am able to test my solutions separately .
Links to my polygon problem is as follow , you can create your own mashup and test it yourself.
If you do not wish to do that you can see screenshots of my problems that contain every information you need.
test constraints are in the images above , please see before proceeding.
I do not consider 0ms and 15ms to be so much difference on codeforces , so both of them wil be considered 0ms.
INPUT
Every of the solution will follow this idea: we iterate through every input and add up into 64-bit integer then after that we mod it by 1,000,000,007
C/C++
1.Standard scanf
Code#include <stdio.h>
int main()
{
long long sum=0;
int n,i,t;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d",&t);
sum+=t;
}
printf("%lld",sum%1000000007);
return 0;
}
(GNU GCC 5.1.0) Time used on tests : #1 0ms #2 0ms #3 0ms #4 0ms #5 78ms #6 155ms #7 764ms #8 1512ms
(GNU GCC C11 5.1.0) Time used on tests : #1 0ms #2 0ms #3 0ms #4 31ms #5 78ms #6 156ms #7 764ms #8 1528ms
(GNU G++ 5.1.0) Time used on tests : #1 0ms #2 0ms #3 0ms #4 0ms #5 93ms #6 156ms #7 764ms #8 1497ms
(GNU G++11 5.1.0) Time used on tests : #1 0ms #2 0ms #3 0ms #4 0ms #5 78ms #6 156ms #7 764ms #8 1513ms
(GNU G++14 6.2.0) Time used on tests : #1 0ms #2 0ms #3 0ms #4 0ms #5 78ms #6 171ms #7 779ms #8 1544ms
(MVC++ 2010) Time used on tests : #1 0ms #2 0ms #3 0ms #4 0ms #5 93ms #6 171ms #7 717ms #8 1419ms
Seems like scanf works the same regardless of compliers.
2.Standard cin
Code#include <iostream>
using namespace std;
int main()
{
long long sum=0;
int n,i,t;
cin>>n;
for(i=1;i<=n;i++)
{
cin>>t;
sum+=t;
}
cout<<sum%1000000007;
return 0;
}
(GNU G++ 5.1.0) Time used on tests : #1 0ms #2 0ms #3 0ms #4 171ms #5 810ms #6 1622ms #7 8876ms #8 >15000ms(TLE)
(GNU G++11 5.1.0) Time used on tests : #1 0ms #2 0ms #3 0ms #4 171ms #5 826ms #6 1637ms #7 8112ms #8 >15000ms(TLE)
(GNU G++14 6.2.0) Time used on tests : #1 0ms #2 0ms #3 0ms #4 140ms #5 655ms #6 1294ms #7 6239ms #8 12619ms
(MVC++ 2010) Time used on tests : #1 0ms #2 0ms #3 0ms #4 78ms #5 343ms #6 655ms #7 3369ms #8 6723ms
LOOK AT THIS!! FOR INTEGERS USING MVC++ 2010 COMPLIER IS FASTER THAN OTHERS
3.cin with ios_base::sync_with_stdio(false); and cin.tie(NULL);
Code#include <iostream>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
long long sum=0;
int n,i,t;
cin>>n;
for(i=1;i<=n;i++)
{
cin>>t;
sum+=t;
}
cout<<sum%1000000007;
return 0;
}
(GNU G++ 5.1.0) Time used on tests : #1 0ms #2 0ms #3 0ms #4 61ms #5 249ms #6 499ms #7 2432ms #8 4773ms
(GNU G++ 5.1.0) Time used on tests : #1 0ms #2 0ms #3 0ms #4 46ms #5 249ms #6 483ms #7 2432ms #8 4835ms
(GNU G++14 6.2.0) Time used on tests : #1 0ms #2 0ms #3 0ms #4 30ms #5 109ms #6 218ms #7 1091ms #8 2167ms
(MVC++ 2010) Time used on tests : #1 0ms #2 0ms #3 0ms #4 62ms #5 327ms #6 639ms #7 3135ms #8 6255ms
LOOK AT THIS!! FOR INTEGERS USING G++14 COMPLIER IS FASTER THAN OTHERS and MVC++ 2010 had no differences compare to standard cin
4.Using getchar() , modified code from geek4geeks.org
Code#include <stdio.h>
void fastscan(int* number)
{
int negative=0;
register int c;
*number = 0;
c = getchar();
while(c!='-'&&(c<'0')||c>'9')
c=getchar();
if (c=='-')
{
negative = 1;
c = getchar();
}
for (; (c>47 && c<58); c=getchar())
*number = *number *10 + c - 48;
if (negative==1)
*number *= -1;
}
int main()
{
int n,i,i2;
long long sum=0;
fastscan(&n);
for(i=1;i<=n;i++)
{
fastscan(&i2);
sum+=i2;
}
sum%=1000000007;
printf("%I64d",sum);
return 0;
}
This will work for both positive and negative 32-bit integers , so you might want to try it out.
(GNU GCC 5.1.0) Time used on tests : #1 0ms #2 0ms #3 0ms #4 0ms #5 30ms #6 46ms #7 140ms #8 295ms
(GNU GCC C11 5.1.0) Time used on tests : #1 0ms #2 0ms #3 0ms #4 0ms #5 0ms #6 30ms #7 139ms #8 264ms
(GNU G++ 5.1.0) Time used on tests : #1 0ms #2 0ms #3 0ms #4 0ms #5 0ms #6 31ms #7 140ms #8 280ms
(GNU G++11 5.1.0) Time used on tests : #1 0ms #2 0ms #3 0ms #4 0ms #5 0ms #6 30ms #7 139ms #8 280ms
(GNU G++14 6.2.0) Time used on tests : #1 0ms #2 0ms #3 0ms #4 31ms #5 140ms #6 296ms #7 1403ms #8 2761ms
(MVC++ 2010) Time used on tests : #1 0ms #2 0ms #3 0ms #4 30ms #5 109ms #6 218ms #7 1122ms #8 2261ms
Something is definitely wrong with G++14,MVC++ 2010 complier with getchar() , so you want to avoid these compilers if you use this fastscan method :) (Try it for yourself , this is amazing!!)
5.Fast and Furious custom by Al.Cash
Please refer to this blog: http://codeforces.me/blog/entry/45835
And my main() here:
Codeint main()
{
input.reset(new InputFile(stdin, false));
output.reset(new OutputFile());
int n,i,t;
long long sum=0;
read(n);
for(i=1;i<=n;i++)
{
read(t);
sum+=t;
}
printf("%I64d",sum%1000000007);
return 0;
}
Requires C++11 and does not work on MVC++
(GNU G++11 5.1.0) Time used on tests : #1 0ms #2 0ms #3 0ms #4 0ms #5 0ms #6 0ms #7 139ms #8 280ms
(GNU G++14 6.2.0) Time used on tests : #1 0ms #2 0ms #3 0ms #4 0ms #5 0ms #6 31ms #7 139ms #8 280ms
JAVA (Tested by bhishma)
1.BufferedReader and String.spit
Codeimport java.util.*;
import java.io.*;
public class testing {
/*
BufferedReader and String.spit
*/
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int mod = (int)(1e9) + 7;
int N = Integer.parseInt(br.readLine());
String tokens[] = br.readLine().split(" ");
int sum = 0;
for(String num : tokens)
sum = (sum + Integer.parseInt(num) ) % mod;
br.close();
System.out.println(sum);
}
}
(JAVA 8) Time used on tests : #1 77ms #2 77ms #3 93ms #4 140ms #5 296ms #6 452ms #7 1653ms #8 3119ms
2.BufferedReader and StringTokenizer
Codeimport java.util.*;
import java.io.*;
public class testing {
/*
* BufferedReader and StringTokenizer
*/
/************************ SOLUTION STARTS HERE ************************/
private static void solve() {
int mod = (int) (1e9) + 7;
int N = nextInt();
int sum = 0;
while (N-- > 0)
sum = (sum + nextInt()) % mod;
println(sum);
}
/************************ SOLUTION ENDS HERE ************************/
/************************ TEMPLATE STARTS HERE **********************/
public static void main(String[] args) throws IOException {
reader = new BufferedReader(new InputStreamReader(System.in));
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)), false);
st = null;
solve();
reader.close();
writer.close();
}
static BufferedReader reader;
static PrintWriter writer;
static StringTokenizer st;
static String next() {
while (st == null || !st.hasMoreTokens()) {
try {
String line = reader.readLine();
if (line == null) {
return null;
}
st = new StringTokenizer(line);
} catch (Exception e) {
throw new RuntimeException();
}
}
return st.nextToken();
}
static String nextLine() {
String s = null;
try {
s = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return s;
}
static int nextInt() {
return Integer.parseInt(next());
}
static long nextLong() {
return Long.parseLong(next());
}
static double nextDouble() {
return Double.parseDouble(next());
}
static char nextChar() {
return next().charAt(0);
}
static int[] nextIntArray(int n) {
int[] a = new int[n];
int i = 0;
while (i < n) {
a[i++] = nextInt();
}
return a;
}
static long[] nextLongArray(int n) {
long[] a = new long[n];
int i = 0;
while (i < n) {
a[i++] = nextLong();
}
return a;
}
static int[] nextIntArrayOneBased(int n) {
int[] a = new int[n + 1];
int i = 1;
while (i <= n) {
a[i++] = nextInt();
}
return a;
}
static long[] nextLongArrayOneBased(int n) {
long[] a = new long[n + 1];
int i = 1;
while (i <= n) {
a[i++] = nextLong();
}
return a;
}
static void print(Object o) {
writer.print(o);
}
static void println(Object o) {
writer.println(o);
}
/************************ TEMPLATE ENDS HERE ************************/
}
(JAVA 8) Time used on tests : #1 93ms #2 109ms #3 78ms #4 108ms #5 155ms #6 264ms #7 873ms #8 1606ms
3.Custom InputReader
Codeimport java.util.*;
import java.io.*;
/*
* Custom InputReader
*/
public class testing {
public static void main(String[] args) throws IOException {
InputReader in = new InputReader();
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
solve(in, out);
out.close();
}
private static void solve(InputReader s1, PrintWriter out) {
int mod = (int) (1e9) + 7;
int N = s1.nextInt();
int sum = 0;
while (N-- > 0)
sum = (sum + s1.nextInt()) % mod;
out.println(sum);
}
static class InputReader {
private byte[] buf = new byte[16384];
private int curChar;
private int numChars;
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = System.in.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public String nextString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public long nextLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int nextInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = nextInt();
}
return arr;
}
public long[] nextLongArray(int n) {
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = nextLong();
}
return arr;
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
}
}
(JAVA 8) Time used on tests : #1 93ms #2 93ms #3 78ms #4 108ms #5 93ms #6 139ms #7 280ms #8 452ms
OUTPUT
C/C++
1.Standard printf
Code#include <stdio.h>
int main()
{
int n,i;
scanf("%d",&n);
for(i=1;i<=n;i++)
printf("%d\n",i);
return 0;
}
(GNU GCC 5.1.0) Time used on tests : #1 0ms #2 0ms #3 0ms #4 31ms #5 202ms #6 1138ms #7 2292ms
(GNU GCC C11 5.1.0) Time used on tests : #1 0ms #2 0ms #3 46ms #4 421ms #5 4367ms #6 >15000ms(TLE) #7 >=15000ms(TLE)
(GNU G++ 5.1.0) Time used on tests : #1 0ms #2 0ms #3 0ms #4 31ms #5 217ms #6 1169ms #7 2324ms
(GNU G++11 5.1.0) Time used on tests : #1 0ms #2 0ms #3 0ms #4 31ms #5 218ms #6 1169ms #7 2308ms
(GNU G++14 6.2.0) Time used on tests : #1 0ms #2 0ms #3 0ms #4 0ms #5 202ms #6 1154ms #7 2262ms
(MVC++ 2010) Time used on tests : #1 0ms #2 0ms #3 0ms #4 31ms #5 233ms #6 1232ms #7 2386ms
AVOID GCC C11 if possible!!
2.Standard cout
Code#include <iostream>
using namespace std;
int main()
{
int n,i;
cin>>n;
for(i=1;i<=n;i++)
cout<<i<<"\n";
return 0;
}
(GNU G++ 5.1.0) Time used on tests : #1 0ms #2 0ms #3 0ms #4 62ms #5 514ms #6 2464ms #7 4944ms
(GNU G++11 5.1.0) Time used on tests : #1 0ms #2 0ms #3 0ms #4 46ms #5 514ms #6 2433ms #7 4913ms
(GNU G++14 6.2.0) Time used on tests : #1 0ms #2 0ms #3 0ms #4 30ms #5 187ms #6 888ms #7 1684ms
(MVC++ 2010) Time used on tests : #1 0ms #2 0ms #3 0ms #4 77ms #5 577ms #6 2916ms #7 5834ms
LOOK AT THIS!! FOR INTEGERS USING G++14 COMPLIER IS FASTER THAN OTHERS , EVEN PRINTF ITSELF?
3.cout with ios_base::sync_with_stdio(false);
Code#include <iostream>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
int n,i;
cin>>n;
for(i=1;i<=n;i++)
cout<<i<<"\n";
return 0;
}
(GNU G++ 5.1.0) Time used on tests : #1 0ms #2 0ms #3 0ms #4 46ms #5 452ms #6 2293ms #7 4742ms
(GNU G++11 5.1.0) Time used on tests : #1 0ms #2 0ms #3 0ms #4 46ms #5 468ms #6 2292ms #7 4616ms
(GNU G++14 6.2.0) Time used on tests : #1 0ms #2 0ms #3 0ms #4 30ms #5 139ms #6 717ms #7 1403ms
(MVC++ 2010) Time used on tests : #1 0ms #2 0ms #3 0ms #4 62ms #5 697ms #6 2932ms #7 5943ms
LOOK AT THIS!! FOR INTEGERS USING G++14 COMPLIER IS FASTER THAN OTHERS , EVEN PRINTF ITSELF? Also MVC++ 2010 does not seems to profit compare to standard cout
4.Using putchar()
Code#include <stdio.h>
void pint(int n)
{
if(n==0)
return;
pint(n/10);
putchar(n%10+'0');
}
int main()
{
int n,i;
scanf("%d",&n);
for(i=1;i<=n;i++)
pint(i),putchar('\n');
return 0;
}
My code only work for positive integers!!
(GNU GCC 5.1.0) Time used on tests : #1 0ms #2 0ms #3 0ms #4 0ms #5 62ms #6 327ms #7 686ms
(GNU GCC C11 5.1.0) Time used on tests : #1 0ms #2 0ms #3 0ms #4 31ms #5 61ms #6 342ms #7 655ms
(GNU G++ 5.1.0) Time used on tests : #1 0ms #2 0ms #3 0ms #4 0ms #5 62ms #6 373ms #7 686ms
(GNU G++11 5.1.0) Time used on tests : #1 0ms #2 0ms #3 0ms #4 0ms #5 46ms #6 327ms #7 670ms
(GNU G++14 6.2.0) Time used on tests : #1 0ms #2 0ms #3 0ms #4 46ms #5 373ms #6 2058ms #7 4102ms
(MVC++ 2010) Time used on tests : #1 0ms #2 0ms #3 0ms #4 30ms #5 296ms #6 1715ms #7 3213ms
So G++14 and MVC++ 2010 also have problem with putchar too :(. Anyway , I encouraged you to modify my code to be able to use with negative integer as well :)
5.Fast and Furious custom by Al.Cash
Please refer to this blog: http://codeforces.me/blog/entry/45835
And my main() here:
Codeint main()
{
input.reset(new InputFile(stdin, false));
output.reset(new OutputFile());
int n,i,t;
long long sum=0;
read(n);
for(i=1;i<=n;i++)
writeln(i);
return 0;
}
Requires C++11 and does not work on MVC++
(GNU G++11 5.1.0) Time used on tests : #1 0ms #2 0ms #3 0ms #4 0ms #5 62ms #6 296ms #7 577ms
(GNU G++14 6.2.0) Time used on tests : #1 0ms #2 0ms #3 0ms #4 0ms #5 61ms #6 296ms #7 592ms
JAVA (Tested by bhishma)
1.System.out.print()
Codeimport java.util.*;
public class Main {
/*
System.out.print()
*/
public static void main(String args[]) {
Scanner s1 = new Scanner(System.in);
int N = s1.nextInt();
s1.close();
for(int i=1;i<=N;i++)
System.out.println(i);
}
}
(JAVA 8) Time used on tests : #1 109ms #2 139ms #3 186ms #4 592ms #5 4945ms #6 >15000ms(TLE) #7 >15000ms(TLE)
2.PrintWriter
Codeimport java.io.PrintWriter;
import java.util.Scanner;
public class testing {
public static void main(String args[]) {
PrintWriter out = new PrintWriter(System.out);
Scanner s1 = new Scanner(System.in);
int N = s1.nextInt();
for(int i=1;i<=N;i++)
out.println(i);
out.close();
s1.close();
}
}
(JAVA 8) Time used on tests : #1 93ms #2 124ms #3 139ms #4 140ms #5 296ms #6 904ms #7 1590ms
3.System.out.print() with StringBuilder
Codeimport java.util.*;
public class Main {
/*
System.out.print with StringBuilder
*/
public static void main(String args[]) {
StringBuilder sb = new StringBuilder();
Scanner s1 = new Scanner(System.in);
int N = s1.nextInt();
s1.close();
for(int i=1;i<=N;i++)
sb.append(i + "\n");
System.out.print(sb);
}
}
(JAVA 8) Time used on tests : #1 108ms #2 93ms #3 109ms #4 124ms #5 249ms #6 810ms #7 1497ms
4.System.out.print() with StringBuilder but clears the StringBuilder buffer after certain writes
Codeimport java.util.*;
public class testing {
/*
System.out.print with StringBuilder but clears the StringBuilder buffer
after certain writes (bufSize)
*/
public static void main(String args[]) {
int bufSize = (int)(1e5);
StringBuilder sb = new StringBuilder();
Scanner s1 = new Scanner(System.in);
int N = s1.nextInt();
s1.close();
for(int i=1;i<=N;i++) {
sb.append(i + "\n");
if(sb.length() >= bufSize) {
System.out.print(sb);
sb = new StringBuilder();
}
}
if(sb.length() > 0)
System.out.print(sb);
}
}
(JAVA 8) Time used on tests : #1 124ms #2 124ms #3 124ms #4 124ms #5 264ms #6 763ms #7 1418ms
5.PrintWriter+StringBuilder
Codeimport java.io.*;
public class testing {
public static void main(String args[]) throws IOException {
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();
int N = Integer.parseInt(br.readLine());
for (int i = 1; i <= N; i++)
sb.append(i + "\n");
out.print(sb);
out.close();
br.close();
}
}
(JAVA 8) Time used on tests : #1 93ms #2 78ms #3 108ms #4 108ms #5 233ms #6 779ms #7 1482ms
UPD1: Added putchar()/getchar() method for C/C++
UPD2: Added tests for MVC++ 2010 compiler
UPD3: Added JAVA tests , thanks to bhishma
UPD4: Added fast and furious C/C++ tests , thanks to Al.Cash