如何提高性能?

发布于 2024-09-06 09:06:34 字数 750 浏览 10 评论 0原文

我准备了一个制作软件应用程序的项目。它是完整的并且工作正常,除了执行速度非常慢..我已经采取了几块代码并优化了它..

我尝试了psyco..即我安装了psyco并在我的代码顶部添加了两行

import psyco
psyco.full()

Don'不知道这是否是使用psyco的方式..如果这是错误的。请告诉我如何使用 psyco.. 因为我添加了这个并没有发现任何改进..

我已经尝试过分析,我知道代码行需要时间,但这些无法进一步优化,并且是不可避免的代码行..

我也认为使用一些 python 包重写“c”中的代码的选项..但是我在使用不属于基本 python 的附加 python 包时总是有非常糟糕的经历..

我正在使用 python 2.6 和 windows vista..请请告诉方法显着提高整个代码执行速度的方法..至少5倍..拜托..

我还没有在方法中编写我的代码,你之间几乎没有方法..没有main..

是的,很少有人建议我是一个 IO 限制问题.. 因为我需要调用代码大约 500 次,这涉及每次调用至少打开和关闭 2 个文件..

在这里,当打开 .pm 文件时,它有两个列,我只需要第一列,所以我将整个第一列复制到列表中并将其传递给函数以获取其行号,然后打开其他文件以将该行号的元素放入列表中...

这是我想要的任务...我想将第一列的元素加载到列表中是很耗时的任何想法来纠正这个问题..

如何提高 IO 绑定瓶颈的性能

拼命寻求帮助

I had prepared a project on making a software application. It is complete and working fine except that the speed of execution is very slow.. I have taken several chunks of code and optimized it..

I tried psyco.. ie I installed psyco and added two lines on the top of my code

import psyco
psyco.full()

Don't know whether this is the way using psyco.. if this is wrong. Please tell me how to use psyco.. because I added this and found no improvement..

I have tried profiling and I know the code lines taking time but these can't be further optimized and are unavoidable line of code..

I also thought of option of rewriting the code in 'c' using some python package.. but I always had a very bad experience in using additional package of python which are not part of basic python..

I am using python 2.6 and windows vista.. please kindly tell methods method for increasing the speed of execution of the whole code significantly.. at least 5x times.. please..

I haven't written my code in method, there few method in between thou.. there is no main..

Yes as few suggested my is an IO bound problem.. as I need to call the code some 500 times and this involves opening and closing of files of at least 2 per call..

And here when opening a .pm file, it has two columns and I need the first columns only, so I am copying the entire first columns into the list and passing it to a function to get its row number and then opening other file to get the elements of that row number into a list...

This is the task I wanted... I guess loading the elements of first columns into the list is time consuming any idea to rectify this..

How can I improve the performance for IO bound bottlenecks

Looking for help desperately

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

看海 2024-09-13 09:06:34

如果您可以切换到二进制文件格式,您可以获得更好的性能。您的大部分代码都在进行解析和字符串操作。您正在执行大量将字符串转换为浮点数的操作,这比您想象的要慢。

You could get a lot better performance if you could switch to binary file formats. Most of your code is doing parsing and string manipulation. You're doing a lot of converting strings to floats, which is slower than you think.

御守 2024-09-13 09:06:34

仅通过调整代码不太可能看到 5 倍的性能差异。

首先,您应该考虑改进您的算法 - 您是否使用适合该工作的最佳数据结构?也许在正确的位置使用 dictset 可以大大加快代码速度。

编写 C 模块并不那么困难,如果您找不到改进 Python 代码的方法,那么这是另一种选择。通常,使用 C 代码您会期望速度提高 5 倍以上。

也许你的问题是 IO 限制。然后您需要寻找提高 IO 性能的方法。

如果您在这里需要更多帮助,您可能必须展示一些代码或至少描述您的程序的功能。

更新:
看起来您正在打开和关闭大量文件,而这些文件在 Windows 上速度往往非常慢。

You are unlikely to see a 5x performance difference by just tweaking the code around.

First you should look at improving your algorithm - are you using the best datastructures for the job? Perhaps using a dict or a set in the right place can speed your code up a alot.

Writing a C module is not all that hard, and is another option if you can find no way to improve the Python code. Usually you would expect more than a 5x speed up by using C code.

Maybe your problem is IO bound. Then you need to look at ways to improve the performance of the IO

If you want more help here, you'll probably have to show some code or at least describe what your program does.

UPDATE:
Looks like you are opening and closing lots of files which tends to be painfully slow on windows.

心在旅行 2024-09-13 09:06:34

psyco 的使用方式就像导入并调用 psyco.full() 一样简单。所以你对 psyco 用法的看法是正确的。

如果您尝试使用 C/C++ 构建 python 模块,请查看 boost::python

您确实应该发布代码以供进一步分析。

psyco can be used as simple as import and call psyco.full(). so you are right about your psyco usage.

If you are trying to build a python module using C/C++, have a look at boost::python

You should really post your code for further analysis.

春花秋月 2024-09-13 09:06:34

要优化代码以提高速度,您只需对其进行分析并查看问题出在哪里。猜测没有帮助。但是一旦你知道了哪里,最划算的通常来自于按降序排列的那些:改进算法,使用更合适的数据结构,消除资源瓶颈(io,内存,cpu),减少内存分配,减少上下文切换(进程和进程)子程序)。

To optimize your code for speed you simply have to profile it and see where the problem is. Guessing does not help. But once you know where, the most bang for your buck usually come from those in descending order: improving algorithm, using more appropriate data structures, removing resource bottlenecks (io,memory,cpu), reducing memory allocation, reducing context switching (processes and subroutines).

梦里南柯 2024-09-13 09:06:34

这是一个优化的机会:您使用非常相似的参数调用 get_list 两次:

join_cost_index_end[index] = get_list(file, float(abs1), fout)
join_cost_index_strt[index] = get_list(file, float(abs2), fout)

这意味着 get_list 中的大部分工作都无缘无故地完成了两次。重写它,以便 get_list 被调用一次,并让它同时返回index_end和index_strt。

Here's one opportunity for optimization: you're calling get_list twice, with very similar arguments:

join_cost_index_end[index] = get_list(file, float(abs1), fout)
join_cost_index_strt[index] = get_list(file, float(abs2), fout)

That means that most of the work in get_list is being done twice for no good reason. Rewrite it so that get_list is being called once, and have it return both index_end and index_strt at the same time.

新雨望断虹 2024-09-13 09:06:34

为什么机器人尝试使用 cython ?您应该在不更改任何代码的情况下获得更好的性能。通过一点点修改,这应该会有更多帮助。

why bot just try using cython? You should get much better performance without changing any of the code. With a little bit of modification this should help even more.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文