パーソナルブログメモリ

a = [1, 1]
for _ in "*" * 999: a += [sum(a[-2:])]
print(a)

pythonにしたら遅い

2017-03-17 | pygame
最近作っているものをpythonにもしてみました。

CPU AMD 7860K
ubuntu 16.04
python3.5.2

pygame利用
pip3 install pygameでインストールしたもの

1フレーム2.5秒ぐらいかかります。

world.png


hi.png プログラムに合わせて一部加工しています。

画像引用元 国土地理院 地球地図全球版
画像のCopyright. Geospatial Information Authority of Japan. ALL RIGHTS RESERVED.

<参考サイト>
Pygameで遊んでみる
Pygameで背景画像の描画
図形を描画する
Pythonで2次元配列の静的確保と動的確保
for文とrange関数を使った指定回数の繰り返し
9.2. math — 数学関数
ミリ秒・マイクロ秒単位で処理時間を計測するには
なぜ変数に値があるのに UnboundLocalError が出るのですか?
pygame documentation
pygame ドキュメント日本語版


test.py
# -*- coding:utf-8 -*-
import pygame
from pygame.locals import *
import math
import sys
import time

px=180
py=150
pt=0#muki
ph=2000#height

sc=1000
wx=373
wy=186
ty=0
t=0
hland = [[0 for i in range(186)] for j in range(373)]

def main():
  global px
  global t
  #hland = [[0 for i in range(186)] for j in range(373)]
  cp = [0 for i in range(40)]
  cr = [0 for i in range(40)]
  cg = [0 for i in range(40)]
  cb = [0 for i in range(40)]
  pygame.init()                               
  screen = pygame.display.set_mode((1400, 700))  
  bg = pygame.image.load("world.png").convert_alpha()    
  bghi = pygame.image.load("hi.png").convert_alpha()     

  hi=[-20,-20,-20,-20,-20,-20,-20,-20,-20,-10,0,20,40,60,80,100,125,150,175,200,225,250,275,300,350,400,450,500,600,700,800,900,1000,1500,2000,3000,4000,5000,6000,7000]

  for i in range(2,41):
    c=bghi.get_at((i*10,40))
    cp[i-2]=c
    cr[i-2]=c.r
    cg[i-2]=c.g
    cb[i-2]=c.b
  for x in range(373):
    for y in range(186):
      c=bg.get_at((x,y))
      r=c.r
      g=c.g
      b=c.b
      bs=10000
      bh=0
      for j in range(40):
        s=abs(r-cr[j])+abs(g-cg[j])+abs(b-cb[j])
        if s<bs:
          bs=s
          hland[x][y]=hi[j]+40

  c1 = bg.get_at((100,100));
  rect_bg = bg.get_rect()
  pygame.display.set_caption("Test")          
  while (1):
    screen.fill((0,255,255))                   
    theworld(screen,bg)
    pygame.display.update()                 # 画面を更新
    px=px+1
    t=t+0.1
    # イベント処理
    for event in pygame.event.get():
      if event.type == QUIT:
        pygame.quit()
        sys.exit()

def theworld(screen,bg):
  starttime = time.time()
  el=1400
  eh=10000
  ez=0
  ex=0
  ep=0
  hwix=700;
  hwiy=200;
  mx=0
  my=0
  cot=math.cos(t)
  sit=math.sin(t)
  for y in range(3,500):
    ez=el*eh/y
    for x in range(1400):
      ex=(x-hwix)*ez/el
      tx=(ex*cot-ez*sit)/sc
      ty=(ex*sit+ez*cot)/sc
      mx=math.floor(px+tx) 
      my=math.floor(py-ty)
      if mx<0 or my<0 or mx>=wx or my>=wy:
        continue
      h=math.floor(hland[mx][my]*5/(math.sqrt(tx*tx+ty*ty)))
      pygame.draw.line(screen, bg.get_at((mx,my)), (x,y+hwiy-h), (x,y+hwiy))

  print(str(time.time()-starttime) + "")

if __name__ == "__main__":                      
    main()

セミコロン一部取り忘れてる。

実行後


最新の画像もっと見る

コメントを投稿

ブログ作成者から承認されるまでコメントは反映されません。