Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- Bounces ten balls around a window
- '''
- from __future__ import division, print_function
- import os
- import random
- import sys
- import pyglet
- from pyglet.gl import *
- window = pyglet.window.Window(640, 480, vsync=True)
- BALL_IMAGE = 'ball.png'
- class Ball(pyglet.sprite.Sprite):
- ball_image = pyglet.resource.image(BALL_IMAGE)
- width = ball_image.width
- height = ball_image.height
- def __init__(self):
- x = random.random() * (window.width - self.width)
- y = random.random() * (window.height - self.height)
- super(Ball, self).__init__(self.ball_image, x, y, batch=balls_batch)
- self.dx = (random.random() - 0.5) * 300
- self.dy = (random.random() - 0.5) * 300
- def update(self, dt):
- if self.x <= 0 or self.x + self.width >= window.width:
- self.dx *= -1
- if self.y <= 0 or self.y + self.height >= window.height:
- self.dy *= -1
- self.x += self.dx * dt
- self.y += self.dy * dt
- self.x = min(max(self.x, 0), window.width - self.width)
- self.y = min(max(self.y, 0), window.height - self.height)
- pyglet_time = 0
- dt_estimated = None
- dt_samples = [0.0] * 100
- num_dt_samples = 0
- deviation_percent = [0] * 250
- fn_stat = None
- num_draw_calls = 0
- def wait_fps_stabilization(dt):
- global fn_stat
- if pyglet_time > 2.0:
- fn_stat = sample_for_estimation
- # print(fn_stat)
- def sample_for_estimation(dt):
- global num_dt_samples, fn_stat, dt_estimated, num_draw_calls
- dt_samples[num_dt_samples] = dt
- num_dt_samples += 1
- if num_dt_samples >= len(dt_samples):
- dt_estimated = sum(dt_samples) / len(dt_samples)
- num_dt_samples = 0
- num_draw_calls = 0
- fn_stat = collect_deviation
- # print(fn_stat)
- def collect_deviation(dt):
- global num_dt_samples, deviation_percent
- num_dt_samples += 1
- bucket = int(abs((dt - dt_estimated) / dt_estimated) * 100)
- if bucket >= len(deviation_percent):
- bucket = len(deviation_percent)-1
- deviation_percent[bucket] += 1
- fn_stat = wait_fps_stabilization
- @window.event
- def on_draw():
- global num_draw_calls
- window.clear()
- num_draw_calls +=1
- balls_batch.draw()
- def update(dt):
- global pyglet_time
- pyglet_time += dt
- #fn_stat(dt)
- for ball in balls:
- ball.update(dt)
- ##@window.event
- ##def on_close(*args):
- ## global deviation_percent, dt_estimated, num_draw_calls
- ## deviation_percent = [v / num_dt_samples * 100 for v in deviation_percent]
- ## print('dt_estimated:', dt_estimated)
- ## print('num_draw_calls:', num_draw_calls)
- ## print('num_dt_samples:', num_dt_samples)
- ## print("dt diferences with estimated real dt, in %")
- ## print(deviation_percent)
- balls_batch = pyglet.graphics.Batch()
- balls = [ Ball() for i in range(10)]
- # the schedule style used in pyglet's noisy.py -> smooth movement, max 2% cpu
- #pyglet.clock.schedule_interval(update, 1/60.)
- # the schedule style update used in cocos actions -> smooth movement, 50% cpu in dual core
- pyglet.clock.schedule(update)
- pyglet.app.run()
- ## stats with deviation clamped to 100 %
- ##stats with schedule interval:
- ##[39.47001394700139, 30.264993026499305, 7.252440725244072, 4.602510460251046, 2.
- ##510460251046025, 1.394700139470014, 0.41841004184100417, 0.8368200836820083, 0.6
- ##97350069735007, 1.2552301255230125, 0.2789400278940028, 0.2789400278940028, 0.13
- ##94700139470014, 0.0, 0.0, 0.41841004184100417, 0.5578800557880056, 0.27894002789
- ##40028, 0.1394700139470014, 0.0, 0.5578800557880056, 0.0, 0.1394700139470014, 0.1
- ##394700139470014, 0.0, 0.0, 0.2789400278940028, 0.0, 0.2789400278940028, 0.0, 0.0
- ##, 0.0, 0.0, 0.1394700139470014, 0.0, 0.1394700139470014, 0.0, 0.0, 0.0, 0.139470
- ##0139470014, 0.8368200836820083, 2.3709902370990235, 0.9762900976290098, 0.836820
- ##0836820083, 0.8368200836820083, 0.9762900976290098, 0.2789400278940028, 0.0, 0.0
- ##, 0.1394700139470014, 0.0, 0.0, 0.0, 0.1394700139470014, 0.0, 0.0, 0.0, 0.0, 0.0
- ##, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0
- ##, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0
- ##, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
- ##
- ##
- ##stats with schedule:
- ## [51.02669404517454, 10.882956878850102, 29.774127310061605, 3.1827515400410675,
- ##1.8480492813141685, 0.5133470225872689, 0.3080082135523614, 0.10266940451745381,
- ## 0.5133470225872689, 0.7186858316221766, 0.0, 0.20533880903490762, 0.0, 0.0, 0.0
- ##, 0.0, 0.0, 0.0, 0.0, 0.0, 0.10266940451745381, 0.0, 0.0, 0.10266940451745381, 0
- ##.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.20533880903490762, 0.0, 0.0,
- ##0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
- ##0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
- ##0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
- ##0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.10266940
- ##451745381, 0.41067761806981523]
- ##
- ## stats with deviation clamped to 250 %
- ##dt_estimated: 0.0302887169153 -> seria fps =33,015594645241027094409941328863
- ##num_draw_calls: 2074
- ##num_dt_samples: 2073
- ##dt diferences with estimated real dt, in %
- ##[3.2320308731307286, 8.007718282682104, 27.399903521466474, 28.268210323203085,
- ##8.05595754944525, 2.2672455378678245, 0.8200675349734683, 0.3376748673420164, 0.
- ##3376748673420164, 0.6271104679208876, 1.4954172696575012, 1.6401350699469366, 0.
- ##6271104679208876, 0.482392667631452, 0.2894356005788712, 0.3376748673420164, 0.2
- ##41196333815726, 0.1929570670525808, 0.6271104679208876, 1.0130246020260492, 0.33
- ##76748673420164, 0.5306319343945972, 0.0964785335262904, 0.3859141341051616, 0.14
- ##47178002894356, 0.2894356005788712, 0.0482392667631452, 0.1447178002894356, 0.04
- ##82392667631452, 0.241196333815726, 0.2894356005788712, 0.3859141341051616, 0.530
- ##6319343945972, 0.1929570670525808, 0.0, 0.0482392667631452, 0.0482392667631452,
- ##0.0964785335262904, 0.723589001447178, 1.8330921369995177, 2.894356005788712, 1.
- ##3989387361312107, 0.5306319343945972, 0.8200675349734683, 1.3506994693680656, 0.
- ##0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0482392667631452, 0.0, 0.0482392667631452, 0.04823
- ##92667631452, 0.0, 0.1447178002894356, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.
- ##0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.
- ##0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.
- ##0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.
- ##0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.
- ##0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.
- ##0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.
- ##0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.
- ##0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.
- ##0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.
- ##0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.
- ##0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.
- ##0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
- ##stats with schedule:
- ##los fps estimados serian 59,978476672184653552338310649317
- ##dt_estimated: 0.0166726475143
- ##num_dt_samples: 2359
- ##dt diferences with estimated real dt, in %
- ##[53.624417125900806, 13.310724883425179, 23.61169987282747, 4.196693514200932, 1
- ##.0173802458668928, 0.5510809665112336, 0.5934718100890208, 0.3391267486222976, 0
- ##.3391267486222976, 0.38151759220008474, 0.1695633743111488, 0.1271725307333616,
- ##0.1695633743111488, 0.0847816871555744, 0.0847816871555744, 0.0423908435777872,
- ##0.0847816871555744, 0.0423908435777872, 0.0423908435777872, 0.0, 0.1271725307333
- ##616, 0.0423908435777872, 0.0423908435777872, 0.0423908435777872, 0.0, 0.04239084
- ##35777872, 0.0847816871555744, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.08478168
- ##71555744, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0423908435777872,
- ##0.0423908435777872, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.04239084
- ##35777872, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0423908435777872,
- ##0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
- ##0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0423908435777
- ##872, 0.0423908435777872, 0.0, 0.0, 0.0, 0.0423908435777872, 0.0, 0.0, 0.0, 0.0,
- ##0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
- ##0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
- ##0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
- ##0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
- ##0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0847816871555744, 0.0, 0.0, 0.0423908435777
- ##872, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0423908435777872, 0.0, 0.0, 0.0, 0.0, 0.0,
- ##0.0, 0.0, 0.0, 0.0423908435777872, 0.1271725307333616, 0.0, 0.0423908435777872,
- ##0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
- ##0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
- ##0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.04239084
- ##35777872]
- ## usando schedule
- ##C:\sync2\_working_now\pyglet schedule>py -2.7 noisy_schedule_interval.py
- ##dt_estimated: 0.0166772762424 corresponderia a fps = 59,961829825521413106889246354743
- ##num_draw_calls: 3646
- ##num_dt_samples: 3645
- ##dt diferences with estimated real dt, in %
- ##[50.75445816186557, 11.193415637860083, 28.31275720164609, 3.7037037037037033, 0
- ##.823045267489712, 0.7681755829903978, 0.43895747599451307, 0.6035665294924555, 0
- ##.35665294924554186, 0.5212620027434842, 0.24691358024691357, 0.19204389574759945
- ##, 0.1646090534979424, 0.0823045267489712, 0.19204389574759945, 0.109739368998628
- ##27, 0.054869684499314134, 0.0, 0.0, 0.0, 0.027434842249657067, 0.027434842249657
- ##067, 0.0823045267489712, 0.027434842249657067, 0.0, 0.027434842249657067, 0.0274
- ##34842249657067, 0.027434842249657067, 0.0823045267489712, 0.027434842249657067,
- ##0.054869684499314134, 0.027434842249657067, 0.0, 0.027434842249657067, 0.0274348
- ##42249657067, 0.0, 0.10973936899862827, 0.0, 0.054869684499314134, 0.0, 0.0548696
- ##84499314134, 0.054869684499314134, 0.0, 0.0, 0.0, 0.0, 0.0, 0.027434842249657067
- ##, 0.0, 0.027434842249657067, 0.054869684499314134, 0.027434842249657067, 0.0, 0.
- ##0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.027434842249657067, 0.0, 0.054869684499314134
- ##, 0.027434842249657067, 0.0, 0.027434842249657067, 0.0, 0.0, 0.0, 0.0, 0.0274348
- ##42249657067, 0.0, 0.0, 0.027434842249657067, 0.0, 0.027434842249657067, 0.027434
- ##842249657067, 0.054869684499314134, 0.054869684499314134, 0.0, 0.0, 0.0, 0.0, 0.
- ##0, 0.027434842249657067, 0.0, 0.0, 0.0, 0.027434842249657067, 0.0, 0.0, 0.0, 0.0
- ##, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.027434842249657067, 0.0, 0.027434842
- ##249657067, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0274348
- ##42249657067, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.
- ##0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.
- ##0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0274348422
- ##49657067, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
- ##0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
- ##0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0274348422496
- ##57067, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0
- ##, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.027434842249657067, 0.0, 0.0, 0.0, 0.0, 0
- ##.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0
- ##.0, 0.0]
Advertisement
Add Comment
Please, Sign In to add comment