当前位置: 首页 > 知识库问答 >
问题:

有什么方法可以加快打印阵列的速度吗?

晁砚
2023-03-14

所以我做了这个程序,你可以给出一个圆或一条线的参数,它会通过在显示器上画一个数组来显示所述对象。

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

提前谢谢!

#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;
}

共有1个答案

欧阳斌
2023-03-14

正如我在评论中所说的,这可能与堆栈溢出问题和答案有关

在阅读了一些内容之后,您还可以尝试缓冲stdout以使其更快。

 类似资料:
  • 问题内容: 我一直对使用print语句简单地输出到终端需要多长时间感到惊讶/沮丧。在经历了最近令人痛苦的缓慢日志记录之后,我决定进行调查,并惊讶地发现几乎 所有 的时间都在等待终端处理结果。 可以以某种方式加快对stdout的写入速度吗? 我编写了一个脚本(此问题底部的’ ‘)来比较将100k行写入stdout,文件以及将stdout重定向到时的时序。计时结果如下: 哇。为了确保python在幕后

  • 问题内容: 根据设计,每次运行新测试时,Selenium都会为您的Firefox配置文件创建一个新副本。我发现此复制时间是一个相当大的瓶颈,尤其是在运行100多个测试时。(5-15秒以重新复制配置文件)。 有谁知道有任何超越行为吗?我希望我的Selenium服务器只重用相同的firefox配置文件。我知道这违反了“干净地设置您的测试装置”的理念,但这是我愿意采取的捷径,因为我的测试不会实质性地改变

  • 问题内容: 考虑以下python程序: 在我的6GB文本文件上运行它,大约2分钟即可完成。 问题: 是否可以更快? 请注意,以下情况需要相同的时间: 因此,我怀疑我的疑问只是一个简单的“否”。 还要注意,我的真实程序正在做的事情不仅仅是计数行数,因此请给出一个通用的答案, 而不是 行数计数技巧(例如在文件中保留行数元数据) PS:我将此问题标记为“ linux”,因为我仅对特定于linux的答案感

  • 问题内容: 据说Java在性能方面比python快10倍。我也从基准测试中看到了这一点。但是真正使Java崩溃的是JVM的启动时间。 这是我做的测试: 相同的文件,Docx和Python中的12 KB ms XLSX嵌入式文件快25倍!WTH! Java需要2.055秒。 我知道这都是由于启动时间造成的,但是我需要通过脚本调用它来解析一些我不想重新发明python的文档。 但是对于解析10k +文

  • 问题内容: 我很好奇如何快速进行元组的for循环。 我知道要访问每个成员,您可以使用带点号的索引号 //错误:类型不符合协议顺序 问题答案: 是的你可以! 瞧! 注意最后一个是不是一个元组这样什么也不会发生(尽管它是一个内容可以被访问1元组或“单” ,为0)。 有趣的是,它甚至可以迭代其他类型的集合。 并且该集合包括和! 注意:作为块的第二个参数传递的值是type 。您必须将其强制转换回原始类型的

  • 本文向大家介绍加快Android手机速度的方法,包括了加快Android手机速度的方法的使用技巧和注意事项,需要的朋友参考一下 没有人喜欢运行缓慢的智能手机,但是随着时间的流逝,您可能已经注意到曾经快速运行的Android设备的运行速度大大降低了。有许多技巧和窍门可以加快您的Android设备的运行速度,并使它像新设备一样运行。他们之中有一些是: 清除缓存的数据 Android应用程序会不断缓存少