【问题标题】:Is there any way to speed up printing arrays?有什么方法可以加快打印阵列的速度吗?
【发布时间】:2019-04-20 16:18:46
【问题描述】:

所以我制作了这个程序,您可以在其中提供圆或线的参数,它会通过在显示器上绘制数组来显示所述对象。

它通过将坐标系“投影”到数组上来工作。 (程序还要求您提供数组的分辨率,列数和行数相同。)然后对于数组的每个单元格,它会检查圆/线是否与单元格相交。如果是,或者它在给定范围内,则单元格将获得值 1。如果超出范围,它将为 0。当所有单元格都被赋予值时,程序将显示数组。所以最后你会看到一个由 1 组成的圆或线,其余的数组将显示为零。

问题是打印数组需要较长时间(7 到 10 秒),而实际计算几乎不需要时间。

我的问题如标题所述,可以以某种方式加快显示数组的过程吗?还是我做错了什么?我使用 Code::Blocks 作为我的编译器。

我知道我的代码可能优化得很差,但我一周前才开始编程。如果代码难以理解,请见谅。

提前谢谢你!

#include <stdio.h>
#include <stdlib.h>

int main()
{
    float x = 0, y = 0, ypos= 0 , xpos = 0, radius = 0, rsqrd = 0, rcheck = 0, thick = 0, grad = 0, offs = 0, lcheck = 0;
    int matsize = 0, i, j, branch = 0;
    char filled;


    printf("\n0 - circle\n1 - line\nDo you want to draw a circle or a line? (0/1) ");
    scanf("%d", &branch);
    if(branch == 0)
    {
        printf("Value of radius: ");
        scanf("%f", &radius);
        printf("Position of circle on the x axis: ");
        scanf("%f", &xpos);
        printf("Position of circle on the y axis: ");
        scanf("%f", &ypos);
        printf("Is the circle filled? (y/n) ");
        scanf(" %c", &filled);
        if(filled == 'n')
        {
            printf("The thickness of circle: ");
            scanf("%f", &thick);
        }
        if(filled == 'y' || filled == 'n')
        {
            printf("Resolution: ");
            scanf("%d" , &matsize);
            printf("\n");
        }


    rsqrd = radius*radius; //rsqrd is equal to radius squared.
    x = -1*(matsize/2); //with this I make sure that the x and y values start from the top right corner of the matrix, so that each x, y value corresponds to the correct cell position (i, j)
    y = matsize/2;
    int mat[matsize][matsize];


    if(filled == 'n')
    {
        for(i = 0; i < matsize; i++)
        {
            for(j = 0; j < matsize; j++)
            {
                rcheck = ((y - ypos)*(y - ypos)) + ((x - xpos)*(x - xpos)); // calculating the equation of the circle with the x and y values taking the offset into account
                if(abs(rcheck-rsqrd) <= (thick*thick))
                {
                    mat[i][j] = 1;
                }
                else
                {
                    mat[i][j] = 0;
                }
                x = x+1; //stepping the values of x and y so they stay with the corresponding cell
            }
            x = -1*(matsize/2);
            y = y-1;
        }
    }
    if(filled =='y')
    {
        for(i = 0; i < matsize; i++)
        {
            for(j = 0; j < matsize; j++)
            {
                rcheck = ((y - ypos)*(y - ypos)) + ((x - xpos)*(x - xpos)); // calculating the equation of the circle with the x and y values taking the offset into account
                if(rcheck <= rsqrd)
                {
                    mat[i][j] = 1;
                }
                else
                {
                    mat[i][j] = 0;
                }
                x = x+1; //stepping the values of x and y so they stay with the corresponding cell
            }
            x = -1*(matsize/2);
            y = y-1;
        }
    }


    if(filled == 'y' || filled == 'n')
    {
        for(i = 0; i < matsize; i++)     // displaying the matrix
        {                                //
            for(j = 0; j < matsize; j++) //
            {                            //
                printf("%d ",mat[i][j]); //
            }                            //
            printf("\n");                //
        }                                //
    }
}
if(branch == 1)
{
    printf("Value of gradient: ");
    scanf("%f", &grad);
    printf("Value of offset: ");
    scanf("%f", &offs);
    printf("Thickness of line: ");
    scanf("%f", &thick);
    printf("Resoultion: ");
    scanf("%d", &matsize);


    x = -1*(matsize/2); //with this I make sure that the x and y values start from the top right corner of the matrix, so that each x, y value corresponds to the correct cell position (i, j)
    y = matsize/2;
    int mat[matsize][matsize];


    for(i = 0; i < matsize; i++)
    {
            for(j = 0; j < matsize; j++)
            {
                lcheck = y - (x * grad); // calculating the equation of the circle with the x and y values taking the offset into account
                if(abs(lcheck-offs) <= thick)
                {
                    mat[i][j] = 1;
                }
                else
                {
                    mat[i][j] = 0;
                }
                x = x+1; //stepping the values of x and y so they stay with the corresponding cell
            }
            x = -1*(matsize/2);
            y = y-1;
    }


    if(branch == 1)
    {
        for(i = 0; i < matsize; i++)    // displaying the matrix
        {                               //
            for(j = 0; j < matsize; j++)//
            {                           //
                printf("%d ",mat[i][j]);// 
            }                           //
            printf("\n");               //
        }                               //
    }
}


return 0;
}

【问题讨论】:

  • 在另一个堆栈溢出问题中,该问题与“为什么打印 A 字符比 B 需要更长的时间?”相关。 (不是那些特定的字符,而是两个不同的字符)可能有一些您可能会觉得有用的信息。一旦我找到这个问题,我就会给你链接。
  • 第一个问题是“你告诉编译器使用什么优化?” (-Ofast-O3 用于 gcc/clang 或 /Ox 用于 VS)(这些都是“哦”,而不是“零”)
  • 还有——您尝试使用什么分辨率。对于基本的终端尺寸(例如Resolution: 50 x 50),圆圈的渲染是即时的(几乎)这也取决于您的终端对输出的处理。 Linux xterms 在历史上是很快的,而 Windows 终端(Win7 和更早版本)却是出了名的慢——Win10 在这方面做了很好的改进..
  • 那么在全部完成后您实际上可以看到多少阵列?通常不会太多,并且完全浪费了编写和滚动所有文本所花费的时间。如此简单的解决方案,只需打印它的尾部即可。或者真正的解决方案,写入文本文件而不是屏幕,这样您就可以看到所有内容。

标签: c arrays matrix


【解决方案1】:

正如我在评论中所说,可能与this stack overflow question and answer有关

阅读后,您也可以尝试缓冲您的stdout 以使其更快。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-12
    • 1970-01-01
    • 1970-01-01
    • 2011-10-22
    • 2016-02-16
    • 2015-08-30
    相关资源
    最近更新 更多