-
you can use your own fast input and output function .
inline void Scan_f(int a)
{
char c = 0;
while(c<33)
//c = fgetc_unlocked(stdin);
c = getc(stdin);
a = 0;
while(c>33)
{
a = a*10 + c - '0';
//c = fgetc_unlocked(stdin);
c = getc(stdin);
}
and in place of scanf("%d",&t) ; you should use this function as Scan_f(&t) ; only.
-
if you are using C++ use ‘\n’ in place of endl , as endl will flush the output buffer , consuming more time .
-
cache locality: accessing memory is faster when successive memory accesses are to nearby locations. A common effect of this is that it’s much faster to iterate over a two-dimensional array with the first index in the outer loop and the second index in the inner loop than vice versa, because this will access elements in linear order in memory and hence hit the cache most of the time, whereas iterating the other way will miss the cache every time, which can result in code that is several times slower.
cache1.c
int main() {
static int x[5000][5000];
for (int i = 0; i < 5000; i++) {
for (int j = 0; j < 5000; j++) {
x[i][j]++;
}
}
return 0;
}
cache2.c
int main() {
static int x[5000][5000];
for (int i = 0; i < 5000; i++) {
for (int j = 0; j < 5000; j++) {
x[j][i]++;
}
}
return 0;
}
results on linux terminal :-
$ gcc -std=c99 -O0 cache1.c -o cache1
$ gcc -std=c99 -O0 cache2.c -o cache2
$ time ./cache1
real 0m0.208s
user 0m0.061s
sys 0m0.062s
$ time ./cache2
real 0m2.251s
user 0m2.027s
sys 0m0.155s
-
Use macros and Inline functions. Calling a function involves overhead which can be avoided by using macros or inline functions, but you need to very careful.
-
Use left/right shift in place of division/multiplication by 2. For example you can replace x = x*4 by x = x<<2.
-
to check if a number is odd in place of if(x%2==1) use if(x&1)
-
if you are passing a vector to a function, use a constant reference.
-
avoid passing arguments by value.