Python 3에서 Sugarscape 에이전트 시뮬레이션 모델의 작은 부분을 복제하려고합니다. 코드 성능이 NetLogo보다 3 배 느립니다. 내 코드에 문제가 있거나 파이썬의 고유 한 제한이 될 수 있습니까?에이전트 기반 시뮬레이션 : 성능 문제 : Python vs NetLogo & Repast
분명히 이것은 코드의 일부분 일 뿐이지 만 파이썬이 런타임의 3 분의 2를 소비하는 곳입니다. 내가이 조각에 표시 할 수 정말 비효율적 뭔가 쓴 경우 나는 희망 :
; -- The SugarScape growth and motion procedures. --
to M ; Motion rule (page 25)
locals [ps p v d]
set ps (patches at-points neighborhood) with [count turtles-here = 0]
if (count ps > 0) [
set v psugar-of max-one-of ps [psugar] ; v is max sugar w/in vision
set ps ps with [psugar = v] ; ps is legal sites w/ v sugar
set d distance min-one-of ps [distance myself] ; d is min dist from me to ps agents
set p random-one-of ps with [distance myself = d] ; p is one of the min dist patches
if (psugar >= v and includeMyPatch?) [set p patch-here]
setxy pxcor-of p pycor-of p ; jump to p
set sugar sugar + psugar-of p ; consume its sugar
ask p [setpsugar 0] ; .. setting its sugar to 0
]
set sugar sugar - metabolism ; eat sugar (metabolism)
set age age + 1
end
내 컴퓨터에서 :
UP = (0, -1)
RIGHT = (1, 0)
DOWN = (0, 1)
LEFT = (-1, 0)
all_directions = [UP, DOWN, RIGHT, LEFT]
# point is just a tuple (x, y)
def look_around(self):
max_sugar_point = self.point
max_sugar = self.world.sugar_map[self.point].level
min_range = 0
random.shuffle(self.all_directions)
for r in range(1, self.vision+1):
for d in self.all_directions:
p = ((self.point[0] + r * d[0]) % self.world.surface.length,
(self.point[1] + r * d[1]) % self.world.surface.height)
if self.world.occupied(p): # checks if p is in a lookup table (dict)
continue
if self.world.sugar_map[p].level > max_sugar:
max_sugar = self.world.sugar_map[p].level
max_sugar_point = p
if max_sugar_point is not self.point:
self.move(max_sugar_point)
대략 동등한 code in NetLogo (이 단편은 위의 파이썬 기능보다 조금 더 않습니다) , 파이썬 코드는 1000 단계를 실행하는 데 15.5 초가 걸립니다. 동일한 랩톱에서 브라우저 내부에서 Java로 실행되는 NetLogo 시뮬레이션은 6 초 이내에 1000 단계를 완료합니다.
EDIT : 자바 구현을 사용하여 Repast를 확인했습니다. 5.4 초에 NetLogo와 거의 같습니다. Java와 Python 사이의 Recent comparisons은 Java에 아무런 이점이 없다는 것을 암시하기 때문에 비난할만한 코드일까요?
편집 : MASON은 Repast보다 더 빠르다고 생각합니다. 그러나 아직까지는 결국 Java가 실행됩니다.
니스. 실행 시간은 15.5에서 11.4 초 (~ 26 %)로 감소했습니다. 도대체 파이썬이 적어도 인터프리터에 -O 옵션을 지정할 때, 그 물건을 최적화하지 않는 이유는 무엇입니까 ??? – max
'점령'기능을 없애고 10.4 초로 떨어졌습니다. – max
@ max : 숙련 된 속도 향상으로 다시 쓰는 것에 대해 감사드립니다. – Curious2learn