0

I'm looking to use a method of outputting a 2D array that is optimized for speed. currently I'm using:

for (int row(0); row < 20; ++row)
    {
        for (int col(0); col < 30; ++col)
        {
            putchar(grid[row][col]);
        }
        putchar('\n');
    }

this works fine however after testing a few things I noticed that using

printf("%s", grid);

I received a massive speed boost, however this was formatted incorrectly as it just output a long string of chars for my array rather than the 30x20 grid I want. I'm wondering if there is any method to get the speed shown with the printf line that formats the grid correctly.

For reference I get about 33ms when using the first method and 1.5ms with the second method.

12
  • If you want speed dont print. Write it to a file or something ... Commented Jan 3, 2017 at 21:18
  • Did you mean to use printf() in the second code? Commented Jan 3, 2017 at 21:18
  • 1
    If you're going to ask for optimizations, always post the compiler options you used to build the code. If optimizations are not used, your timings you posted are worthless. Commented Jan 3, 2017 at 21:18
  • Any function that puts anything onto the screen will be slow, because it needs to access the screen (and everything in between). If you really need that speed up, construct the whole output as a string before calling printf or similar. But your needs looks suspicious to me. Don't try to optimize something that will probably turn out to be useless. Commented Jan 3, 2017 at 21:19
  • @Nelxiost stdio uses buffering, so it doesn't access the screen for every character. Commented Jan 3, 2017 at 21:20

2 Answers 2

4

You can write each row at once using fwrite():

for (int row = 0; row < 20; ++row) {
    fwrite(grid[row], sizeof grid[row], 1, stdout);
    putchar('\n');
}

BTW, your code that writes the whole grid using printf("%s", grid) is most likely causing undefined behavior. %s requires the argument to be a pointer to a null-terminated string, and grid presumably doesn't have a null at the end. If it seems to be working it's just accidental, because there happened to be a null byte in the memory following grid.

Note that this solution only works for an actual 2-D array of char. If it's an array of pointers, you can replace sizeof grid[row] with a variable or macro that specifies the size of each row pointed to by grid[row] (or just a hard--coded 30 as in the question).

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

5 Comments

If grid is dynamically allocated sizeof(grid[row]) will give the size of a pointer. Four or eight bytes depending the system. So in that case the correct answer would be : fwrite(grid[row], sizeof grid[row][0], col, stdout).
@nikau6 He described this as a 2D array, not an array of pointers. The fact that printf("%s", grid); printed the whole grid supports this (although I suppose it's possible that the rows were located adjacent to each other when they were dynamically allocated).
A 2D array can be a 2D array of pointer. And I'm not saying that he's using a dynamically allocated array, I'm only giving the information that in the case the 2D array is dynamically allocated it'll not works.
And even if the rows are located adjacent to each other it'll return the size of a pointer in case its dynamically allocated. Cause grid[n] contains a address not a element. grid[n] contains a address, and grid[n][0] contains the first element of a line.
I've added an addition to my answer that addresses this.
2

if you make each of you lines terminate with a '\0' char, you can do that :

for(int row=0; row<20; ++row)
{
    printf("%s\n", grid[row]);
}

3 Comments

He said that printing the entire grid seemed to work, except it didn't put newlines between the rows. That means there's no nulls in the middle.
I said that if it add a zero at the end of each lines what I propose will work
Sorry, I misread it, I thought you were saying that if he already did that.

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.