简体   繁体   English

C 几次读入后程序变慢

[英]C Program slowing down after a couple read-ins

So i have to write a C program for class, that solves a connect four like game, that roughly said resolves matches of same colored pieces, lets pieces above fall down, then checks again for matches, if no matches were found it drops the next piece and so on.所以我必须为 class 编写一个 C 程序,它解决了一个像游戏一样的连接四,粗略地说解决了相同颜色的棋子的匹配,让上面的棋子掉下来,然后再次检查匹配,如果没有找到匹配它下降下一个片等。

My given input is a text file, where each line holds information about the color and the x-position, where the piece is supposed to fall down.我给定的输入是一个文本文件,其中每一行都包含有关颜色和 x 位置的信息,该位置应该是棋子掉落的位置。 My implementation is based on AVL-trees and my program works fine for inputs roughly up to 2.000.000 lines and solves it in about 3 seconds.我的实现基于 AVL 树,我的程序可以很好地处理大约 2.000.000 行的输入,并在大约 3 秒内解决它。 One bigger example file i have to solve is roughtly 2.600.000 lines long but doesnt terminate.我必须解决的一个更大的示例文件大约有 2.600.000 行长,但不会终止。 The program reads in about 1.000.000 lines per second but right after about the 2.000.000 mark its slows down tremendously and only reads in a couple 100 lines per second.该程序每秒读取大约 1.000.000 行,但在大约 2.000.000 标记之后它的速度大大降低并且每秒仅读取 100 行。

Its one of my first "bigger" projects and has about 900 lines of code, so i dont see the point in posting it here (unless someone really wants to see it)它是我第一个“更大”的项目之一,大约有 900 行代码,所以我不认为将它发布在这里有什么意义(除非有人真的想看到它)

I am really clueless what the cause could be, so maybe someone of you has an idea or can point me in a direction for things i have to look out for.我真的不知道原因可能是什么,所以也许你们中的某个人有想法或者可以指出我必须注意的方向。

Add logging with timestamps to spot where the slowdown occurs.添加带有时间戳的日志记录以发现减速发生的位置。 You need to isolate the code that has the performance issue.您需要隔离有性能问题的代码。

The first step to getting better is to measure where you currently are.变得更好的第一步是衡量你目前所处的位置。 Once you know how long the current process takes, you can decide if a change you make helps, hurts or makes no change.一旦您知道当前过程需要多长时间,您就可以决定您所做的更改是有帮助、有害还是没有改变。

There's a vast world of software optimization, look around for expensive loops.有广阔的软件优化世界,四处寻找昂贵的循环。 Read up on big oh notation.阅读大哦符号。 If you have a loop that processes a loop that processes a loop O(n^3), what seems fast with 100 iterations each can slow down dramatically when each loop is a million.如果您有一个循环处理一个处理循环 O(n^3) 的循环,那么当每个循环为一百万时,100 次迭代看起来很快的循环可能会显着减慢。

If you have a performance profiler it can help organize the timestamps from the logs it creates so that the code that's taking the most time is highlighted.如果你有一个性能分析器,它可以帮助组织它创建的日志中的时间戳,以便突出显示花费最多时间的代码。 Many times what happens in cases like this is that something that seems good enough when you start out isn't good enough once you start to scale.在这种情况下,很多时候发生的情况是,开始时看起来足够好的东西一旦开始扩展就不够好。

Nobody says you have to add log to every single line of your source code.没有人说您必须将日志添加到源代码的每一行。 That can be overwhelming.那可能是压倒性的。 Figuring out what you don't have to focus on can be very helpful.弄清楚您不必关注的内容可能会非常有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM