Slow printf: why does __USE_MINGW_ANSI_STDIO=0 make it faster? - c

I wrote the code for a problem in codeforces and even though I believe I was doing it in the best time complexity it was exceeding the time limit on the 7th test case. After some testing, it seemed to me that the major amount of time was being taken by printf, which seemed odd since using printf some 3 * 10^5 times shouldn't be such a big deal. So I searched a lot and found this: https://codeforces.com/blog/entry/105687#comment-940911
Now I made the conclusion that using this line at the top of my code will make printf faster:
#define __USE_MINGW_ANSI_STDIO 0
So I ran my code with the above included and voila what was exceeding the time limit of 1s earlier now with the inclusion of just one line of code got accepted in merely 62 ms.
I didn't understand most of the other stuff that was talked about in the link like MinGW implementations and all.
So my question is, firstly why does it work this way? Secondly, can I/should I keep using the above line of code in all my programs on codeforces from now on?
P.S. I also found this blog: https://codeforces.com/blog/entry/47180
It was too confusing for me to grasp for the time being but maybe someone else can understand it and shed some light on the matter.
Also, here is the codeforces problem: https://codeforces.com/contest/1774/problem/C
Here is my solution:
https://codeforces.com/contest/1774/submission/185781891
I don't know the entire input as codeforces doesn't share it and it'd be very very big. But I know that the value inputted to the tests variable is 3, the values inputted to n[0], n[1], n[2] are 100000, 100000, 100000
Here is my code:
#define __USE_MINGW_ANSI_STDIO 0
#include <stdio.h>
#include <stdlib.h>
// #include <math.h>
// #include <string.h>
// #define lint long long int
// Function Declarations
int main(void)
{
int tests;
scanf("%i", &tests);
int **answers = malloc(tests * sizeof(int*));
int *n = malloc(sizeof(int) * tests);
for (int i = 0; i < tests; i++)
{
scanf("%i", &n[i]);
char *enviro = malloc((n[i]) * sizeof(int));
answers[i] = malloc((n[i] - 1) * sizeof(int));
int consec = 1; // No. of same consecutive elements at the very
// end.
scanf("%s", enviro);
answers[i][0] = 1; // Case where x = 2;
for (int x = 3; x < n[i] + 1; x++)
{
// comparing corresponding to current x vs previous x
if (enviro[x - 2] == enviro[x - 3])
{
consec++;
}
else
{
consec = 1;
}
answers[i][x - 2] = x - consec;
}
// Free loop variables
free(enviro);
}
/* if (tests == 3)
{
printf("n[%i] = %i\n", i, n[i]);
} */
for (int i = 0; i < tests; i++)
{
for (int j = 0; j < n[i] - 1; j++)
{
printf("%i ", answers[i][j]);
}
printf("\n");
free(answers[i]);
}
// Free variables
free(answers);
return 0;
}
EDIT: So I tried the following code for the same problem on codeforces (https://codeforces.com/contest/1774/submission/185788962) just to see the execution time:
// #define __USE_MINGW_ANSI_STDIO 0
#include <stdio.h>
#include <math.h>
int main(void)
{
int n = pow(10, 5);
for (int i = 0; i < n; i++)
{
printf("*");
}
}
Without the #define __USE_MINGW_ANSI_STDIO 0 it gave an e.t. of 374ms. With it, it gave e.t. of 15ms.

It seems like MinGW defined their own printf() functions, __mingw_printf(). This is done to fix format specifiers' problems on some old Windows operating systems, as seen in their wikis. The macro __USE_MINGW_ANSI_STDIO is set to 0 if you don't want to use MinGW's implementation, and 1 if you do.
It also seems like MinGW's implementation is slower, so not using it will make your code faster.

Related

Why does the Code run on Linux but not on Windows?

Well, I wrote a little Program that should generate random values, but no value should be in the output file twice.
On Linux it's running perfectly, but on Windows it just runs infinity long on the 32768th value.
That means, that cmd is open but nothing really happens from that point.
I already did debug it 30 times by now but it never had any problem (it was hell do debug it)
I wrote it new, recompiled it, even changed values under it was running through the debugger
Here is the Code:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
int intlen(int[]);
int main(int argc, char *argv[]) {
FILE *fp;
int percent = 0;
int Ergebnis = 0, length = 0, lenNow = 0;
bool proof = true;
srand(time(NULL));
fp = fopen("ranGen.txt", "w");
length = atoi(argv[1]);
int Lookup[length];
for (int x = 0; x < length; x++){
Lookup[x] = 0;
}
for (int i = 0; i < length; i++) {
do {
proof = true;
Ergebnis = rand() % (2147483646 - 1 + 1) + 1;
for (int j = 0; j < length && Lookup[j] != 0 && proof != false; j++) {
if (Ergebnis == Lookup[j]) {
proof = false;
}
}
}while(proof == false);
Lookup[lenNow] = Ergebnis;
lenNow++;
fprintf(fp,"%i ",Ergebnis);
}
return 0;
}
posted everything, but the output because I don't really know where the problem is and I think you will need the most of it reproduce my problem.
if you compiled it, run it through cmd with something like 50000, so that it is higher than 32768.
(like this: example.exe 50000)
Expected was, that it will create a File named RanGen.txt with 200000 random values (200000 was my test value)
But the output was 32767 Values in the text Document and then the program just did nothing more.
Solution: used rand() % 214748346; instead of rand() % (214748346 - 1 + 1) + 1;
Looks like rand() is only 16 bits in that library. Make it 32 bits by calling it twice:
int rand32() {
return rand() ^ (rand() << 16);
}
Also, consider eliminating the inner duplicate-search loop by using Bob Floyd's algorithm: https://blog.acolyer.org/2018/01/30/a-sample-of-brilliance/

Online judgeProblem (Wrong Answer)

problem in Light OJ:
1001 - Opposite Task:
This problem gives you a flavor the concept of special judge. That means the judge is smart enough to verify your code even though it may print different results. In this problem you are asked to find the opposite task of the previous problem.
To be specific, I have two computers where I stored my problems. Now I know the total number of problems is n. And there are no duplicate problems and there can be at most 10 problems in each computer. You have to find the number of problems in each of the computers.
Since there can be multiple solutions. Any valid solution will do.
Input:
Input starts with an integer T (≤ 25), denoting the number of test cases.
Each case starts with a line containing an integer n (0 ≤ n ≤ 20) denoting the total number of problems.
Output:
For each case, print the number of problems stored in each computer in a single line. A single space should separate the non-negative integers.
Sample Input
Output for
Sample Input:
3
10
7
7
Sample output:
0(space)10
0(space)7
1(space)6
my code :
#include<stdio.h>
#include <stdlib.h>
int main()
{
int c,sum;
int i,j,mini=0,maxi;
int com1,com2;
do{
scanf("%d",&c);
}while (c>25);
int t[c+1];
for(i=1; i<=c; i++)
{
do{
scanf("%d",&t[i]);
}while (t[i]>20);
}
for(i=1; i<=c; i++)
{
maxi=t[i];
com1=rand() % (maxi - mini + 1) + mini;
com2=t[i]-com1;
printf("%d %d\n",com1,com2);
}
return 0;
}
When I submit my code the judge gives wrong answer.
But while I compile the code in CodeBlocks it gives right answer.
I can't understand the problem in online judge.
How can I solve it.
The original post has mini=0 for all cases, but where the total problems is > 10 that may not work. I have adjusted that
#include<stdio.h>
#include <stdlib.h>
int main()
{
int c = 0, mini, maxi, i, com1, com2, *t;
scanf ("%d",&c);
t = malloc (c * sizeof(int));
for (i=0; i<c; i++)
scanf ("%d", &t[i]);
printf("\n");
for (i=0; i<c; i++) {
if (t[i]>10) {
maxi = 10;
mini = t[i] - 10;
} else {
maxi = t[i];
mini = 0;
}
com1 = rand() % (maxi - mini + 1) + mini;
com2 = t[i] - com1;
printf("%d %d\n",com1,com2);
}
free (t);
return 0;
}
Please note that results of malloc() and scanf() have not been checked: they should be.
You seem to be looping unnecessary number of times for the input. I have removed the unnecessary loops from your code while retaining the logic you used. I made a silly mistake with my earlier answer.
#include<stdio.h>
#include <stdlib.h>
int main()
{
int c = 0;
int maxi=10;
scanf("%d",&c);
int t[c];
for(int i=0; i<c; i++)
scanf("%d",&t[i]);
for(int i=0; i<c; i++)
{
int com1 = t[i],com2 = 0;
if (t[i] > 10)
{
com1 = 10
com2 = t[i]-com1;
}
printf("%d %d\n",com1,com2);
}
return 0;
}

Finding sum of square of two numbers in an array

My program should take a number from the user, and find the two numbers in an array such that the sum of their squares equals the user input squared. However, I'm having trouble doing this, as well as understanding all the errors I'm getting.
Here's my current attempt:
#include <stdio.h>
int numberaa;
scanf("%d",&numberaa);
int main()
{
int i,j;
int array[9] = {2,-4,6,3,9,0,-1,-9};
for (i = 0; i <= 8; i++)
for (j = 0; j <= 8; J++0)
firstone==i*i
secondone==j*j
if {
firstone+secondone=numberaa;
printf("The Numbers are %d and %d",j,i,numberaa);
return 0
};
Change
firstone+secondone=numberaa;
to
numberaa = firstone + secondone;
Ah! You need to grab a basic C book. For this time I am posting a correct code for you. Hope you will learn something.
#include <stdio.h>
int main()
{
int i,j;
int array[9] = {2,-4,6,3,9,0,-1,-9};
int numberaa;
scanf("%d",&numberaa);
for (i = 0; i <= 8; i++){
for (j = 0; j <= 8; J++0){
firstone = i*i
secondone = j*j
if(numberaa == firstone + secondone)
printf("The Numbers are %d and %d",j,i,numberaa);
}
}
return 0
}
You need to read through at least the introductory chapter of a book on C and work through the examples. That means typing them out (no, don't copy and paste), compiling them, and running them to understand what makes them work and what breaks them.
When you write your own code, always compile with warnings enabled, e.g. gcc -Wall -o my_executable_name my_code.c, and pay attention to the line numbers referenced in compiler errors and warnings.
I'll point out some locations of errors in your code below:
#include <stdio.h>
int numberaa; // Currently you're declaring this as a global. NO! not what you want.
scanf("%d",&numberaa); // This isn't going to happen out here. NO! NO NO NO!
int main() // Specify your parameters. int main(void)
{
int i,j;
int array[9] = {2,-4,6,3,9,0,-1,-9}; // why specify an array of 9 but store just 8 elements??
for (i = 0; i <= 8; i++) // These are the correct limits for array[9].
for (j = 0; j <= 8; J++0) // j and J are not the same. What is J++0 ????!! Also, read about "blocks" and try a for-loop example with more than one line.
firstone==i*i // WTF?? Have you even tried to compile this?
secondone==j*j // See line above.
if { // Likewise
firstone+secondone=numberaa; // Likewise again.
printf("The Numbers are %d and %d",j,i,numberaa); // How many formatting flags does your first argument have, and how many are to be inserted?
return 0 }; // again, have you tried to compile this?
Short version:
Semicolons
Assignment vs. equality
Scope of variables
Blocks, brace usage
syntax of if statements
You also aren't squaring the user input.
Efficiency: you only need to calculate firstone = i * i once for each i value, so take it outside the j loop.

Why is this C program crashing?

I have a simple test program in C to scramble an array of values on the heap. Sidenote: I know the random logic here has a flaw that will not allow the "displaced" value to exceed RAND_MAX, but that is not the point of this post.
The point is that when I run the code with N = 10000, every once in a while it will crash with very little information (screenshots posted below). I'm using MinGW compiler. I can't seem to reproduce the crash for lower or higher N values (1000 or 100000 for example).
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
const int N = 10000;
int main() {
int i, rand1, rand2, temp, *values;
/* allocate values on heap and initialize */
values = malloc(N * sizeof(int));
for (i = 0; i < N; i++) {
values[i] = i + 1;
}
/* scramble */
srand(time(NULL));
for (i = 0; i < N/10; i++) {
rand1 = (int)(N*((double)rand()/(double)RAND_MAX));
rand2 = (int)(N*((double)rand()/(double)RAND_MAX));
temp = values[rand1];
values[rand1] = values[rand2];
values[rand2] = temp;
}
int displaced = 0;
for (i = 0; i < N; i++) {
if (values[i] != (i+1)) {
displaced++;
}
}
printf("%d numbers out of order\n", displaced);
free(values);
return 0;
}
it may be because rand() generates a random number from 0 to RAND_MAX inclusive so (int)(N*((double)rand()/(double)RAND_MAX)) can be N, which exceeds the array boundary. however, i don't see why that would vary with array size (it does explain why it only crashes sometimes, though).
try /(1+(double)RAND_MAX) (note that addition is to the double, to avoid overflow, depending on the value of RAND_MAX) (although i'm not convinced that will always work, depending on the types involved. it would be safer to test for N and try again).
also, learn to use a tool from Is there a good Valgrind substitute for Windows? - they make this kind of thing easy to fix (they tell you exactly what went wrong when you run your program).

C program - weird Seg fault in dynamic programming algorithm

I'm writing a program in C to do a simple dynamic programming algorithm where you return the minimum number of coins needed to add up to a certain amount. Here's my code:
#include <limits.h>
#include <stdlib.h>
#include <stdio.h>
/*
This function returns the minimum number of stamps required for a given value.
It assumes that the given array contains the available stamp sizes, and that it
always contains 1, so a solution is always possible
*/
int min_number_of_stamps(const int* array, size_t array_size, int request) {
/* Construct a table with dimensions (array_size+1)*(request+1) */
int numRows = array_size + 1;
int numCols = request + 1;
int **DPtable;
DPtable = malloc(numRows*sizeof(int));
int i;
for (i = 0; i < numRows; i++) {
DPtable[i] = malloc(numCols*sizeof(int));
}
printf("%d",DPtable[4][0]);
int r, c, useIt, loseIt;
for (r = 0; r < numRows; r++) {
for (c = 0; c < numCols; c++) {
printf("%d,%d\n", r, c);
if (c==0) {
printf("1\n");
//if the amount of change is 0, 0 coins are needed
DPtable[r][c] = 0;
}
else if ((r==0) || c < array[r-1]) {
printf("2\n");
//if there are no coins or if the change needed is less than
//the smallest coin available, then 'infinity' coins are needed
DPtable[r][c] = INT_MAX;
}
else {
printf("3\n");
useIt = DPtable[r][c-array[r-1]] + 1;
loseIt = DPtable[r-1][c];
if (useIt <= loseIt) {
//if 'use it' requires fewer coins than 'lose it,' then
//'use it' coins are needed.
DPtable[r][c] = useIt;
}
else {
//if 'lose it' requires fewer coins, 'lose it' coins are needed
DPtable[r][c] = loseIt;
}
}
}
}
return DPtable[numRows][numCols];
}
int main() {
const int array[] = {1,5,10,25};
const int* stamps = &array[0];
printf("%d", min_number_of_stamps(stamps, 4, 44));
}
I'm getting a segfault when my inner for loop gets to the case where r=4 and c=0. I left my debugging print statements in because I'm lazy, but you can see where I got stuck. If I access the same place in the array outside of my for loops, there's no problem. But in the for loop, I get a `Segmentation fault: 11' message after it outputs "4,0" for the array element and "1" for the if case it's in. Can anyone see what I'm missing?
Learn to enable warnings & debugging for your compiler, i.e. gcc -g -Wall on Linux.
Learn to use a debugger, i.e. gdb -tui on Linux.
Consider using valgrind
EDIT
Many tutorials (in several languages, e.g. English, French, ....) for GCC, GDB, and ValGrind are easily found on the Web.
You're allocating dpTable incorrectly. It should be
DPtable = malloc(numRows*sizeof(int*));
See if that fixes the problem.
return DPtable[numRows][numCols];
thats out of bounds isn't it?

Resources