4

I'm just working on a snake game project, but I just realized that the old printf is too slow to display a 15*45 2-dimensional array.

If you know any I/O to output it faster please help me!

My goal is about 0.10 - 0.15 s.

I accept libraries too but I want full copyright :p :p.

for (y = 0; y < MAX_Y ; y++) 
 {
  printf ("\t");
  for (x = 0; x < MAX_X; x++)
   {
    printf ("%c", base[y][x]);  
   }
  printf ("\n");
 }
13
  • 3
    printf() is "slow". you'd be better off building one longer string out of your individual characters and outputting the big string ONCE, rather than outputting each individual character. Commented Jan 30, 2015 at 19:31
  • You can try to only print the part of array that has changed, instead of print the whole array every time you want to update the screen. Commented Jan 30, 2015 at 19:32
  • No Szoke, asking for help and stating you want full copyright in the same question...? Anyway..... learn opengl, or directx (Google them). Or buy an i7 based PC..... and using the console for output... budget for an income of zero. But seriously, google OpenGL.... it's for displaying graphics. Commented Jan 30, 2015 at 19:37
  • I tried that but the prgogram gets comfused whit spaces when I compile. Lol Gavin that was a joke :p and i can program only C Commented Jan 30, 2015 at 19:40
  • printf is as old as for(at least in C)... Commented Jan 30, 2015 at 20:10

2 Answers 2

3

As you don't use real formatting, and print only individual chars you can use the simple putchar() function:

for (y = 0; y < MAX_Y ; y++) 
 {
  putchar ('\t');
  for (x = 0; x < MAX_X; x++)
   {
    putchar(base[y][x]);  
   }
  putchar('\n');
 }

On my machine, iterating the loops 100,000, testing it in 3 runs and redirecting output to /dev/null, gave me:

  • 6.679u, 6.663u and 6.766u with printf,
  • 3.309u, 3.315u and 3.312u with putchar,
  • 0.263u, 0.261u and 0.263u (with putchar_unlocked).

If I use terminal output I have:

  • 8.153u with printf,
  • 3.862u with putchar,
  • 0.634u with putchar_unlocked.

Elapsed times are:

  • 0:09.46 with printf,
  • 0:07.75 with putchar,
  • 0:05.06 with putchar_unlocked.

-Edit----single write---------

You can also group everything into a single string and use puts, like this:

 char baseString[MAX_Y*(MAX_X+2)+1];
 int p = 0;
 for (int y = 0; y < MAX_Y ; y++) 
   {
     baseString[p++] = '\t';
     for (int x = 0; x < MAX_X; x++)
       {
         baseString[p++] = base[y][x];  
       }
     baseString[p++] = '\n';
   }
 baseString[p] = 0;
 puts(baseString); // or fwrite(baseString,p,1,stdout);

In that case, test gives:

  • 0.448u, 1.155s and 0:04.99 (puts)
  • 0.418u, 1.077s, 0:04.81 (fwrite)

This is slightly better than putchar_unlocked.

All test made on OSX 10.9, Intel Core i7 2.3Ghz.

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

6 Comments

Did you measure the speed gain?
On my machine printf version is roughly slower by a factor 2. printf must at least decode two arguments, detect end of string, test for % in format string; so necessary slower.
Is that user time ("u")? If yes, how does real time change, if any? Your times are fast, are you on a native linux? Are there any changes when you redirect to /dev/null (which makes it 10x faster on cygwin)?
I tested redirecting all to /dev/null not to measure device I/O. I am going to give you terminal output times...
Oh. Well, I suggest you don't use 100000 then.
|
0

how about

fwrite(base[y], MAX_X, 1, stdout)

to write one whole row at a time

no I havent tested it for speed

1 Comment

fwrite doesn't writes in a txt file ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.