执行速度问题 [英] Execution speed question

查看:77
本文介绍了执行速度问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在网络(图表)上进行模拟。我有一个关于

执行速度的问题(假设现在内存非常充足)。我在下面简化了模拟的

细节,因为我问的问题比我的具体情况更适用于b $ b。我非常感谢在计算方面的一般

反馈,当然还有特定于Python中实现
实现的注意事项。


节点在我的网络中可能是开或关。所有节点处于OFF状态时,网络以

开始。我遍历节点。对于每个节点

OFF,我会考虑根据其邻居的

状态开启的概率。我必须在决定之前通过所有节点

哪些都可以开启。


所以我的问题是它是否更快到

1.遍历所有节点列表,并使用ifs





来检查OFF节点.2。循环通过一个OFF节点的容器并在它们打开时从它们中移除


第二个会导致循环通过较少的节点,尤其是作为

模拟进展,但删除成本与

便宜的ifs相比如何?额外的内存使用会影响性能。


我是感谢if check的成本,节点的数量,以及我使用的容器类型将会得到答案。


在我的情况下,ifs是廉价的布尔查询(节点是ON还是

OFF)。节点的数量非常大:数百万肯定,可能是数百万美元。如果考虑(2),请注意上面我的BOLD文本,

表示我不能删除节点,因为我在主循环中迭代它们。


我自然开始用(2)编码,但无法决定python的最佳数据结构。一套似乎是快速删除的理想选择,但是后来我无法通过弹出来迭代它们。有序清单?一些

有numpy数组的创意解决方案?


还有一个复杂因素,因为我们在解释python,

什么是从理论上讲,最好的数据结构实际上可能不是最优的,除非它是一个默认的系统对象或在一个外部编码的

编译模块中。


当然,我会开始尝试看看执行

的区别是什么,但我会感谢其他人的一些建议。

这是最好的,也是(2)的最佳数据结构。


我不是新手,所以你可以通过python-wise和

算法明智地获得技术。我意识到这是一个''基本''问题,但这是我一直想知道的事情(廉价的ifs与额外的结构)和

的数量我正在考虑的节点,它实际上成了一个问题。


非常感谢,

Suresh

I am performing simulations on networks (graphs). I have a question on
speed of execution (assuming very ample memory for now). I simplify the
details of my simulation below, as the question I ask applies more
generally than my specific case. I would greatly appreciate general
feedback in terms of computing and of course considerations specific to
implementation in Python.

The nodes in my network may be ON or OFF. The network starts off with
all nodes in the OFF state. I loop through the nodes. For each node
that is OFF, I consider some probability of it turning ON based on the
states of its neighbours. I MUST GO THROUGH ALL NODES BEFORE DECIDING
WHICH ONES TO TURN ON.

So my question is whether it is faster to

1. loop through a list of ALL nodes and check for OFF nodes using ifs

or to

2. loop through a container of OFF nodes and remove from this when they
turn ON

The second would result in looping through less nodes, especially as the
simulation progresses, but how does the cost of removal compare with
cheap ifs and would the extra memory usage affect performance.

I an appreciate that the cost of the if check, the number of nodes, and
the type of container I use will come into the answer.

In my case, the ifs are cheap boolean queries (whether the node is ON or
OFF). The number of nodes is very large: millions for sure, maybe tens
of millions. If considering (2), take note of my BOLD text above, which
means I can''t remove nodes as I iterate through them in the main loop.

I naturally started coding with (2), but couldn''t decide on the best data
structure for python. A set seemed ideal for speedy removal, but then I
can''t iterate through them with out popping. An ordered list? Some
creative solution with numpy arrays?

There is also the complication that since we are in interpreted python,
what is theoretically the best data structure may not in reality be
optimal unless it is a default system object or coded externally in a
compiled module.

Of course, I will start experimenting to see what the execution
difference is, but I would appreciate some suggestions from others re
which is best and also on best data structure for (2).

I''m not a newbie, so you can get technical with me python-wise and
algorithm wise. I realise it is a ''basic'' question, but it is something
that I have always wondered about (cheap ifs versus extra structure) and
with the number of nodes I am considering, it actually becomes an issue.

Many Thanks,
Suresh

推荐答案

7月25日,7:57 * pm,Suresh Pillai< stochash ... @ yahoo.cawrote:
On Jul 25, 7:57*pm, Suresh Pillai <stochash...@yahoo.cawrote:

我网络中的节点可能是开或关。 *网络启动时所有节点都处于OFF状态。

。 *我遍历节点。 *对于每个节点

即OFF,我会考虑根据其邻居的

状态开启的概率。 *我必须在决定之前通过所有节点

才能开启。


所以我的问题是它是否更快到


1.遍历所有节点列表并使用ifs
检查OFF节点
The nodes in my network may be ON or OFF. *The network starts off with
all nodes in the OFF state. *I loop through the nodes. *For each node
that is OFF, I consider some probability of it turning ON based on the
states of its neighbours. *I MUST GO THROUGH ALL NODES BEFORE DECIDING
WHICH ONES TO TURN ON.

So my question is whether it is faster to

1. loop through a list of ALL nodes and check for OFF nodes using ifs



我建议使用''filter''和list comprehensions。

I''d recommend using ''filter'' and list comprehensions.


>> import random
class Node:
>>import random
class Node:



... def __init __(self):

... self.on = False

... def toggle(self):

... self.on = random.choice([True,False])

...

... def __init__(self):
... self.on = False
... def toggle(self):
... self.on = random.choice([True, False])
...


>> nodes = [Node()for i in range(0,10000 )]
节点中的节点:$ b​​ $ b
>>nodes = [Node() for i in range(0, 10000)]
for node in nodes:



... node.toggle()

...

... node.toggle()
...


>> off_nodes = filter(lambda x:不是x.on,节点)
len(off_nodes)
>>off_nodes = filter(lambda x: not x.on, nodes)
len(off_nodes)



5050

5050


我建​​议使用''filter''和列表推导。


看看使用reduce()。您可以收集有关所有

节点的信息,而无需在

流程中构建大型中间列表。


您可以从这里得到一些想法[ http://en.wikipedia.org/wiki/

Antiobjects]。

I''d recommend using ''filter'' and list comprehensions.

Look at using reduce(). You can collect information about all of the
nodes without necessarily building a large, intermediate list in the
process.

You might get some ideas from here [http://en.wikipedia.org/wiki/
Antiobjects].


7月25日上午10:57,Suresh Pillai< stochash ...... @ yahoo.cawrote:
On Jul 25, 10:57 am, Suresh Pillai <stochash...@yahoo.cawrote:

我正在网络(图表)上进行模拟。我有一个关于

执行速度的问题(假设现在内存非常充足)。我在下面简化了模拟的

细节,因为我问的问题比我的具体情况更适用于b $ b。我非常感谢在计算方面的一般

反馈,当然还有特定于Python中实现
实现的注意事项。


节点在我的网络中可能是开或关。所有节点处于OFF状态时,网络以

开始。我遍历节点。对于每个节点

OFF,我会考虑根据其邻居的

状态开启的概率。我必须在决定之前通过所有节点

哪些都可以开启。


所以我的问题是它是否更快到

1.遍历所有节点列表,并使用ifs





来检查OFF节点.2。循环通过一个OFF节点的容器,当它们打开时,从它们移除
打开
I am performing simulations on networks (graphs). I have a question on
speed of execution (assuming very ample memory for now). I simplify the
details of my simulation below, as the question I ask applies more
generally than my specific case. I would greatly appreciate general
feedback in terms of computing and of course considerations specific to
implementation in Python.

The nodes in my network may be ON or OFF. The network starts off with
all nodes in the OFF state. I loop through the nodes. For each node
that is OFF, I consider some probability of it turning ON based on the
states of its neighbours. I MUST GO THROUGH ALL NODES BEFORE DECIDING
WHICH ONES TO TURN ON.

So my question is whether it is faster to

1. loop through a list of ALL nodes and check for OFF nodes using ifs

or to

2. loop through a container of OFF nodes and remove from this when they
turn ON



或3.每次迭代删除时建立一个新的列表来自旧的

一个:


处理时:

new_off_list = []

for x在off_list:

if go_on(x):

on_list.append(x)

else:

new_off_list .append(x)

off_list = new_off_list

generation + = 1

Iain

or 3. build a new list every iteration intead of deleting from the old
one:

while processing:
new_off_list = []
for x in off_list:
if goes_on(x):
on_list.append(x)
else:
new_off_list.append(x)
off_list = new_off_list
generation += 1

Iain


这篇关于执行速度问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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