【问题标题】:Logging the time it takes to fill a LinkedList and Array记录填充 LinkedList 和数组所需的时间
【发布时间】:2015-10-03 09:20:35
【问题描述】:

这个控制台应用程序有点奇怪但有点有趣,如果它有效的话。首先,我计算了用 4.000.000 个元素和随机数 填充 LinkedList 所需的时间。然后我在 LinkedList 中搜索 100 个随机元素。在这之间,我正在写下填充和查找元素所花费的时间。

在那之后,我试图再次做同样的事情,但使用Array。首先填充它,然后寻找 100 个随机元素。然后我对array 进行排序,以查看在未排序和排序array 中查找100 个随机元素之间的区别。然后再次输入时间。

问题是,在我填充LinkedList 并找到LinkedList 中的元素后,我开始用循环填充数组。我得到一个无限循环。真不知道ATM怎么了。

如果您想提供帮助,我建议您复制我粘贴到此问题中的代码,以便您了解它应该如何查找程序的所有部分。

代码:

    public static bool sokning(int[] a, int b)
    {
        bool sant = false;
        Random rand = new Random();
        Stopwatch watchFindArray = new Stopwatch();
        Console.Write("Letar efter tal: ");
        watchFindArray.Start();
        int myint = 0;

        for (int iii = 0; iii < a.Length; iii++)
        {
            b = rand.Next();
            Console.Write("#");
            myint = Array.BinarySearch(a, b);

            if (myint < 0)
            {
                sant = false;
            }
            else
            {
                sant = true;
            }
        }
        watchFindArray.Stop();

        if (sant == true)
        {
            Console.WriteLine("\nFann alla element efter " + watchFindArray.Elapsed.TotalSeconds + " sekunder.");
            return true;
        }
        else
        {
            return false;
        }
    }

    public static void körMetod()
    {
        const int MAX = 40000000;
        int[] array = new int[MAX];
        int hittamig2 = 0;
        Random rand2 = new Random();

        Stopwatch watchArray = new Stopwatch();
        Console.WriteLine("\nStartar Array...");
        watchArray.Start();
        Console.Write("Position: ");
        for (int ii = 0; ii < MAX; ii++)
        {
            array[ii] = rand2.Next();
            if (array.Length % 1000000 == 0)
            {
                Console.Write("#");
            }
        }
        watchArray.Stop();
        Console.WriteLine("\nTid: " + watchArray.Elapsed.TotalSeconds + " sekunder att fylla en array.");
        Console.WriteLine("Letar efter tal: ");
        bool sant = sokning(array, hittamig2);


        Console.WriteLine("Sorterar arrayen.");
        Array.Sort(array);
        sant = sokning(array, hittamig2);

        if (sant == false)
        {
            Console.WriteLine("\nHittade inte alla element i arrayen.");
            Console.ReadLine();
        }
        else
        {
            Console.WriteLine("Klar!");
            Console.ReadLine();
        }
    }


    static void Main(string[] args)
    {
        Random rnd = new Random();
        const int MAX = 40000000;
        LinkedList<int> lankadLista = new LinkedList<int>();
        Stopwatch watchLinkedList = new Stopwatch();
        Console.WriteLine("Startar LinkedList...");
        watchLinkedList.Start();
        Console.Write("Position: ");
        for (int i = 0; i < MAX; i++)
        {
            lankadLista.AddLast(rnd.Next());
            if (lankadLista.Count() % 1000000 == 0)
            {
                Console.Write("#");
            }
        }
        watchLinkedList.Stop();
        Console.WriteLine("\nTid: " + watchLinkedList.Elapsed.TotalSeconds + " sekunder att fylla en LinkedList.");
        Stopwatch watchFindLinkedList = new Stopwatch();
        int hittaMig;
        Console.Write("Letar efter tal: ");
        watchFindLinkedList.Start();
        for (int j = 0; j < 100; j++)
        {
            hittaMig = rnd.Next();
            Console.Write("#");
            lankadLista.Find(hittaMig);
        }
        watchFindLinkedList.Stop();
        Console.WriteLine("\nFann alla element efter " +
        watchFindLinkedList.Elapsed.TotalSeconds + " sekunder.");

        Console.ReadLine();

        körMetod();
    }

最好的问候。

【问题讨论】:

  • 您是否尝试调试它并查看为什么循环条件状态没有改变?
  • 是的,我尝试更改循环值。但奇怪的是,我为 LinkedList 制作了相同的循环,并且有效...

标签: c# arrays sorting linked-list console-application


【解决方案1】:

你不是在无限循环中,问题在于它是以下代码:

for (int ii = 0; ii < MAX; ii++)
{
    array[ii] = rand2.Next();
    if (array.Length % 1000000 == 0)
    {
        Console.Write("#");
    }
}

内部条件是array.Length % 1000000 == 0,它总是true,因为array 的大小在你初始化时总是40000000

const int MAX = 40000000;
int[] array = new int[MAX];

当您执行array[ii] = rand2.Next(); 时,您不会更改数组的长度,您只是将其中一个单元格设置为等于rand2.Next();

这会导致Console.Write("#");每次迭代 中工作,并且还会显着减慢您的循环。

要解决这个问题,只需更改:

if (array.Length % 1000000 == 0)

到:

if (ii % 1000000 == 0)

不想 每次都在数组末尾添加新项目,因为,调整数组大小每次都重新分配数组,这非常慢,但您可以使用Array.Resize 方法来完成(没有理由这样做)

【讨论】:

  • 使用 LinkedList im 使用 .AddLast 方法。数组有类似的东西吗?有点坚持如何解决它的想法。
【解决方案2】:

我认为您在搜索 Array 的例程中存在很大问题。 (sokning)

只搜索 100 个元素的代码在哪里?

您似乎正在搜索一个随机生成的数字 4000 万次。仅修复 Console.Write("#") 以在每一百万点正确写入是不够的。我认为让你认为有无限循环的最大延迟是在你的代码中,它在 4000 万个数字的数组中搜索 4000 万个随机生成的数字

当然这不是很“响应式”(还考虑到你调用了这个方法两次)

public static bool sokning(int[] a, int b)
{
    bool sant = false;
    Random rand = new Random();
    Stopwatch watchFindArray = new Stopwatch();
    Console.Write("Letar efter tal: ");
    watchFindArray.Start();
    int myint = 0;

    // Search only 100 numbers like you do in the linked list
    for (int iii = 0; iii < 100; iii++)
    {
        b = rand.Next();
        Console.Write("#");
        myint = Array.BinarySearch(a, b);

        if (myint < 0)
        {
            sant = false;
        }
        else
        {
            sant = true;
        }
    }
    watchFindArray.Stop();

    if (sant == true)
    {
        Console.WriteLine("\nFann alla element efter " + watchFindArray.Elapsed.TotalSeconds + " sekunder.");
        return true;
    }
    else
    {
        return false;
    }
}

还有两个小问题。

为什么要在sokning 方法中传递变量b?原始值从未使用过,当您开始循环搜索随机生成的数字时,b 变量 os 将被覆盖。所以我认为你可以删除它

第二个问题是这个sokning 方法的结果。在每个循环中将 sant 变量设置为 true 或 false。所以最新的循环获胜。换句话说,如果最新循环找到匹配项,则返回 true,否则返回 false。如果之前的某个循环有不同的结果,那么 sokning 的调用者将完全丢失它。

【讨论】:

    猜你喜欢
    • 2015-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-25
    • 1970-01-01
    • 2023-02-22
    • 1970-01-01
    相关资源
    最近更新 更多