什么可以导致程序第二次运行得更快? [英] What can cause a program to run much faster the second time?

查看:143
本文介绍了什么可以导致程序第二次运行得更快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我写的测试代码时,我注意到的一件事是,长时间运行的操作往往比第一次执行程序运行时更长时间,而不是后续运行,有时是10或10倍更多。显然,这里有一些冷缓存/热缓存问题,但是我似乎无法弄清楚它是什么。



这不是CPU缓存,因为这些长 - 运行操作往往是循环,我将大量的数据提供给,并且它们应该在第一次迭代后被完全加载。 (此外,卸载和重新加载程序应该清除缓存。)



此外,它不是光盘缓存。我已经通过从磁盘上预先加载所有数据并处理它,并且这是实际的CPU限制数据处理正在慢慢地排除。



那么什么可能导致我的程序运行缓慢,我第一次运行它,但是如果我关闭它并运行它,它运行速度更快?我在几个不同的程序中看到了这样的事情,这似乎是一个普遍的问题。



编辑:为了澄清,我在Delphi虽然我并不认为这是一个特定于Delphi的问题。但这意味着无论问题如何,它与JIT问题,垃圾收集问题或托管代码带来的任何其他行李无关。而且我没有处理网络连接。这是纯CPU的处理。



一个例子:脚本编译器。它运行如下:




  • 将整个文件从光盘加载到内存中

  • 将整个文件归入一个令牌队列

  • 将队列解析成树

  • 在树上运行codegen以生成字节码



    • 如果在将整个内容从光盘加载到内存中之后,我将它提供了一个巨大的脚本文件(〜100k行),则在第一次运行时,lex步骤大约需要15秒,后续运行2秒。 (是的,我知道这还是很久,我正在努力工作...)我想知道这个减速来自哪里,我可以做些什么。

      解决方案

      要尝试的三件事:




      • 在抽样分析器中运行,包括冷运行(重新启动后的第一件事)。通常应该是足够的。

      • 检查内存使用情况,它是否变得如此之高(甚至瞬间),操作系统将不得不从RAM中交换内容以为您的应用腾出空间?这可以解释你所看到的。还可以查看启动应用程序时可用RAM的数量。

      • 启用系统性能工具并检查I / O计数器或文件访问,并确保在FileMon / Process Explorer你没有一些您忘记的文件或网络访问(剩余日志/测试代码)


      Something I've noticed when testing code I write is that long-running operations tend to run much longer the first time a program is run than on subsequent runs, sometimes by a factor of 10 or more. Obviously there's some sort of cold cache/warm cache issue here, but I can't seem to figure out what it is.

      It's not the CPU cache, since these long-running operations tend to be loops that I feed a lot of data to, and they should be fully loaded after the first iteration. (Plus, unloading and reloading the program should clear the cache.)

      Also, it's not the disc cache. I've ruled that out by loading all data from disc up-front and processing it afterwards, and it's the actual CPU-bound data processing that's going slowly.

      So what can cause my program to run slow the first time I run it, but then if I close it and run it again, it runs dramatically faster? I've seen this in several different programs that do very different things, so it seems to be a general issue.

      EDIT: For clarification, I'm writing in Delphi, though I don't really think this is a Delphi-specific issue. But that means that whatever the problem is, it's not related to JIT issues, garbage collection issues, or any of the other baggage that managed code brings with it. And I'm not dealing with network connections. This is pure CPU-bound processing.

      One example: a script compiler. It runs like this:

      • Load entire file into memory from disc
      • Lex the entire file into a queue of tokens
      • Parse the queue into a tree
      • Run codegen on the tree to produce bytecode

      If I feed it an enormous script file (~100k lines,) after loading the entire thing from disc into memory, the lex step takes about 15 seconds the first time I run, and 2 seconds on subsequent runs. (And yes, I know that's still a long time. I'm working on that...) I'd like to know where that slowdown is coming from and what I can do about it.

      解决方案

      Three things to try:

      • Run it in a sampling profiler, including a "cold" run (first thing after a reboot). Should usually be enough.
      • Check memory usage, does it grow so high (even transiently) the OS would have to swap things out of RAM to make room for your app? That alone could be an explanation for what you're seeing. Also look at the amount of free RAM you have when you start your app.
      • Enable system performance tools and check the I/O counters or file accesses, and make sure under FileMon / Process Explorer that you don't have some file or network accesses you've forgotten about (leftover log/test code)

      这篇关于什么可以导致程序第二次运行得更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆