benchmark
This commit is contained in:
parent
92a9d87108
commit
72c222fc1f
5 changed files with 350 additions and 2 deletions
18
README.md
18
README.md
|
@ -222,5 +222,21 @@ See test_dynamic.py for how to use.
|
|||
|
||||
* converting more examples from C to python
|
||||
* testing and building on more platforms
|
||||
* sorting out binary wheel distribution for Mac/Win and compile-from-source distributtion for Linux
|
||||
|
||||
# Performance
|
||||
|
||||
For fastest permformance use Pypy rather than standard python.
|
||||
|
||||
Every call to C is costly, so it's slightly faster if you use Python data structures and functions when calculating
|
||||
in your update loop
|
||||
and then only convert them to C data structures when you have to call the C functions for drawing.
|
||||
|
||||
## Bunnymark
|
||||
|
||||
|
||||
| Library | Implementation | Bunnies (60 FPS) | Percentage |
|
||||
| ------------- | ------------- | ------------- | ------------- |
|
||||
| Raylib 3.7 | C | 168100 | 100% |
|
||||
| Raylib Python CFFI 3.7 | Pypy 3.7 | 33800 | 20% |
|
||||
| Raylib Python CFFI 3.7 | Python 3.9 | 6800 | 4% |
|
||||
|
||||
|
|
BIN
examples/textures/resources/wabbit_alpha.png
Normal file
BIN
examples/textures/resources/wabbit_alpha.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 496 B |
111
examples/textures/textures_bunnymark.py
Normal file
111
examples/textures/textures_bunnymark.py
Normal file
|
@ -0,0 +1,111 @@
|
|||
# /*******************************************************************************************
|
||||
# *
|
||||
# * raylib [textures] example - Bunnymark
|
||||
# *
|
||||
# * This example has been created using raylib 1.6 (www.raylib.com)
|
||||
# * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
|
||||
# *
|
||||
# * Copyright (c) 2014-2019 Ramon Santamaria (@raysan5)
|
||||
# *
|
||||
# ********************************************************************************************/
|
||||
|
||||
from raylib.static import *
|
||||
MAX_BUNNIES = 500000
|
||||
|
||||
# This is the maximum amount of elements (quads) per batch
|
||||
# NOTE: This value is defined in [rlgl] module and can be changed there
|
||||
MAX_BATCH_ELEMENTS = 8192
|
||||
|
||||
|
||||
class Bunny:
|
||||
def __init__(self):
|
||||
self.position = ffi.new('struct Vector2 *', [0.0, 0.0])
|
||||
self.speed = ffi.new('struct Vector2 *', [0.0, 0.0])
|
||||
self.color = ffi.new('struct Color *', [0, 0, 0, 0])
|
||||
|
||||
|
||||
# // Initialization
|
||||
# //--------------------------------------------------------------------------------------
|
||||
screenWidth = 1920;
|
||||
screenHeight = 1080;
|
||||
|
||||
InitWindow(screenWidth, screenHeight, b"raylib [textures] example - bunnymark")
|
||||
|
||||
# // Load bunny texture
|
||||
texBunny = LoadTexture(b"resources/wabbit_alpha.png")
|
||||
|
||||
bunnies = []
|
||||
for i in range(0, MAX_BUNNIES):
|
||||
bunnies.append(Bunny())
|
||||
|
||||
bunniesCount = 0; # Bunnies counter
|
||||
|
||||
SetTargetFPS(60); # Set our game to run at 60 frames-per-second
|
||||
#//--------------------------------------------------------------------------------------
|
||||
|
||||
#// Main game loop
|
||||
while not WindowShouldClose(): #// Detect window close button or ESC key
|
||||
#// Update
|
||||
#//----------------------------------------------------------------------------------
|
||||
if IsMouseButtonDown(MOUSE_LEFT_BUTTON):
|
||||
#// Create more bunnies
|
||||
for i in range(0, 100):
|
||||
if bunniesCount < MAX_BUNNIES:
|
||||
bunnies[bunniesCount].position = GetMousePosition()
|
||||
bunnies[bunniesCount].speed.x = GetRandomValue(-250, 250)/60.0
|
||||
bunnies[bunniesCount].speed.y = GetRandomValue(-250, 250)/60.0
|
||||
bunnies[bunniesCount].color = (GetRandomValue(50, 240),
|
||||
GetRandomValue(80, 240),
|
||||
GetRandomValue(100, 240), 255 )
|
||||
|
||||
bunniesCount+=1
|
||||
|
||||
|
||||
# // Update bunnies
|
||||
for i in range(0, bunniesCount):
|
||||
bunnies[i].position.x += bunnies[i].speed.x;
|
||||
bunnies[i].position.y += bunnies[i].speed.y;
|
||||
|
||||
if ((bunnies[i].position.x + texBunny.width/2) > GetScreenWidth()) or ((bunnies[i].position.x + texBunny.width/2) < 0):
|
||||
bunnies[i].speed.x *= -1
|
||||
if ((bunnies[i].position.y + texBunny.height/2) > GetScreenHeight()) or ((bunnies[i].position.y + texBunny.height/2 - 40) < 0):
|
||||
bunnies[i].speed.y *= -1
|
||||
|
||||
# //----------------------------------------------------------------------------------
|
||||
#
|
||||
# // Draw
|
||||
# //----------------------------------------------------------------------------------
|
||||
BeginDrawing()
|
||||
|
||||
ClearBackground(RAYWHITE)
|
||||
|
||||
for i in range(0, bunniesCount):
|
||||
# // NOTE: When internal batch buffer limit is reached (MAX_BATCH_ELEMENTS),
|
||||
# // a draw call is launched and buffer starts being filled again;
|
||||
# // before issuing a draw call, updated vertex data from internal CPU buffer is send to GPU...
|
||||
# // Process of sending data is costly and it could happen that GPU data has not been completely
|
||||
# // processed for drawing while new data is tried to be sent (updating current in-use buffers)
|
||||
# // it could generates a stall and consequently a frame drop, limiting the number of drawn bunnies
|
||||
DrawTexture(texBunny, int(bunnies[i].position.x), int(bunnies[i].position.y), bunnies[i].color)
|
||||
|
||||
DrawRectangle(0, 0, screenWidth, 40, BLACK)
|
||||
text = f"bunnies {bunniesCount}"
|
||||
DrawText(text.encode('utf-8'), 120, 10, 20, GREEN)
|
||||
text = f"batched draw calls: { 1 + int(bunniesCount/MAX_BATCH_ELEMENTS)}"
|
||||
DrawText(text.encode('utf-8'), 320, 10, 20, MAROON)
|
||||
|
||||
DrawFPS(10, 10)
|
||||
|
||||
EndDrawing()
|
||||
#//----------------------------------------------------------------------------------
|
||||
|
||||
|
||||
#// De-Initialization
|
||||
#//--------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
UnloadTexture(texBunny); #Unload bunny texture
|
||||
|
||||
CloseWindow() # Close window and OpenGL context
|
||||
#//--------------------------------------------------------------------------------------
|
||||
|
111
examples/textures/textures_bunnymark_dynamic.py
Normal file
111
examples/textures/textures_bunnymark_dynamic.py
Normal file
|
@ -0,0 +1,111 @@
|
|||
# /*******************************************************************************************
|
||||
# *
|
||||
# * raylib [textures] example - Bunnymark
|
||||
# *
|
||||
# * This example has been created using raylib 1.6 (www.raylib.com)
|
||||
# * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
|
||||
# *
|
||||
# * Copyright (c) 2014-2019 Ramon Santamaria (@raysan5)
|
||||
# *
|
||||
# ********************************************************************************************/
|
||||
|
||||
from raylib.static import *
|
||||
MAX_BUNNIES = 500000
|
||||
|
||||
# This is the maximum amount of elements (quads) per batch
|
||||
# NOTE: This value is defined in [rlgl] module and can be changed there
|
||||
MAX_BATCH_ELEMENTS = 8192
|
||||
|
||||
|
||||
class Bunny:
|
||||
def __init__(self):
|
||||
self.position = ffi.new('struct Vector2 *', [0.0, 0.0])
|
||||
self.speed = ffi.new('struct Vector2 *', [0.0, 0.0])
|
||||
self.color = ffi.new('struct Color *', [0, 0, 0, 0])
|
||||
|
||||
|
||||
# // Initialization
|
||||
# //--------------------------------------------------------------------------------------
|
||||
screenWidth = 1920;
|
||||
screenHeight = 1080;
|
||||
|
||||
InitWindow(screenWidth, screenHeight, b"raylib [textures] example - bunnymark")
|
||||
|
||||
# // Load bunny texture
|
||||
texBunny = LoadTexture(b"resources/wabbit_alpha.png")
|
||||
|
||||
bunnies = []
|
||||
for i in range(0, MAX_BUNNIES):
|
||||
bunnies.append(Bunny())
|
||||
|
||||
bunniesCount = 0; # Bunnies counter
|
||||
|
||||
SetTargetFPS(60); # Set our game to run at 60 frames-per-second
|
||||
#//--------------------------------------------------------------------------------------
|
||||
|
||||
#// Main game loop
|
||||
while not WindowShouldClose(): #// Detect window close button or ESC key
|
||||
#// Update
|
||||
#//----------------------------------------------------------------------------------
|
||||
if IsMouseButtonDown(MOUSE_LEFT_BUTTON):
|
||||
#// Create more bunnies
|
||||
for i in range(0, 100):
|
||||
if bunniesCount < MAX_BUNNIES:
|
||||
bunnies[bunniesCount].position = GetMousePosition()
|
||||
bunnies[bunniesCount].speed.x = GetRandomValue(-250, 250)/60.0
|
||||
bunnies[bunniesCount].speed.y = GetRandomValue(-250, 250)/60.0
|
||||
bunnies[bunniesCount].color = (GetRandomValue(50, 240),
|
||||
GetRandomValue(80, 240),
|
||||
GetRandomValue(100, 240), 255 )
|
||||
|
||||
bunniesCount+=1
|
||||
|
||||
|
||||
# // Update bunnies
|
||||
for i in range(0, bunniesCount):
|
||||
bunnies[i].position.x += bunnies[i].speed.x;
|
||||
bunnies[i].position.y += bunnies[i].speed.y;
|
||||
|
||||
if ((bunnies[i].position.x + texBunny.width/2) > GetScreenWidth()) or ((bunnies[i].position.x + texBunny.width/2) < 0):
|
||||
bunnies[i].speed.x *= -1
|
||||
if ((bunnies[i].position.y + texBunny.height/2) > GetScreenHeight()) or ((bunnies[i].position.y + texBunny.height/2 - 40) < 0):
|
||||
bunnies[i].speed.y *= -1
|
||||
|
||||
# //----------------------------------------------------------------------------------
|
||||
#
|
||||
# // Draw
|
||||
# //----------------------------------------------------------------------------------
|
||||
BeginDrawing()
|
||||
|
||||
ClearBackground(RAYWHITE)
|
||||
|
||||
for i in range(0, bunniesCount):
|
||||
# // NOTE: When internal batch buffer limit is reached (MAX_BATCH_ELEMENTS),
|
||||
# // a draw call is launched and buffer starts being filled again;
|
||||
# // before issuing a draw call, updated vertex data from internal CPU buffer is send to GPU...
|
||||
# // Process of sending data is costly and it could happen that GPU data has not been completely
|
||||
# // processed for drawing while new data is tried to be sent (updating current in-use buffers)
|
||||
# // it could generates a stall and consequently a frame drop, limiting the number of drawn bunnies
|
||||
DrawTexture(texBunny, int(bunnies[i].position.x), int(bunnies[i].position.y), bunnies[i].color)
|
||||
|
||||
DrawRectangle(0, 0, screenWidth, 40, BLACK)
|
||||
text = f"bunnies {bunniesCount}"
|
||||
DrawText(text.encode('utf-8'), 120, 10, 20, GREEN)
|
||||
text = f"batched draw calls: { 1 + int(bunniesCount/MAX_BATCH_ELEMENTS)}"
|
||||
DrawText(text.encode('utf-8'), 320, 10, 20, MAROON)
|
||||
|
||||
DrawFPS(10, 10)
|
||||
|
||||
EndDrawing()
|
||||
#//----------------------------------------------------------------------------------
|
||||
|
||||
|
||||
#// De-Initialization
|
||||
#//--------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
UnloadTexture(texBunny); #Unload bunny texture
|
||||
|
||||
CloseWindow() # Close window and OpenGL context
|
||||
#//--------------------------------------------------------------------------------------
|
||||
|
110
examples/textures/textures_bunnymark_more_pythonic.py
Normal file
110
examples/textures/textures_bunnymark_more_pythonic.py
Normal file
|
@ -0,0 +1,110 @@
|
|||
# Dont use C data structures when we can avoid it. Makes Pypy slightly faster.
|
||||
|
||||
from raylib.static import *
|
||||
import random
|
||||
|
||||
MAX_BUNNIES = 500000
|
||||
|
||||
# This is the maximum amount of elements (quads) per batch
|
||||
# NOTE: This value is defined in [rlgl] module and can be changed there
|
||||
MAX_BATCH_ELEMENTS = 8192
|
||||
|
||||
|
||||
class Bunny:
|
||||
def __init__(self):
|
||||
self.position_x = 0.0
|
||||
self.position_y = 0.0
|
||||
self.speed_x = 0.0
|
||||
self.speed_y = 0.0
|
||||
self.color_r = 0
|
||||
self.color_g = 0
|
||||
self.color_b = 0
|
||||
self.color_a = 0
|
||||
|
||||
|
||||
# // Initialization
|
||||
# //--------------------------------------------------------------------------------------
|
||||
screenWidth = 1920;
|
||||
screenHeight = 1080;
|
||||
|
||||
InitWindow(screenWidth, screenHeight, b"raylib [textures] example - bunnymark")
|
||||
|
||||
# // Load bunny texture
|
||||
texBunny = LoadTexture(b"resources/wabbit_alpha.png")
|
||||
|
||||
bunnies = []
|
||||
for i in range(0, MAX_BUNNIES):
|
||||
bunnies.append(Bunny())
|
||||
|
||||
bunniesCount = 0; # Bunnies counter
|
||||
|
||||
SetTargetFPS(60); # Set our game to run at 60 frames-per-second
|
||||
#//--------------------------------------------------------------------------------------
|
||||
|
||||
#// Main game loop
|
||||
while not WindowShouldClose(): #// Detect window close button or ESC key
|
||||
#// Update
|
||||
#//----------------------------------------------------------------------------------
|
||||
if IsMouseButtonDown(MOUSE_LEFT_BUTTON):
|
||||
#// Create more bunnies
|
||||
for i in range(0, 100):
|
||||
if bunniesCount < MAX_BUNNIES:
|
||||
bunnies[bunniesCount].position_x = GetMousePosition().x
|
||||
bunnies[bunniesCount].position_y = GetMousePosition().y
|
||||
bunnies[bunniesCount].speed_x = random.randint(-250, 250)/60.0
|
||||
bunnies[bunniesCount].speed_y = random.randint(-250, 250)/60.0
|
||||
bunnies[bunniesCount].color_r = random.randint(50,240)
|
||||
bunnies[bunniesCount].color_g = random.randint(80, 240)
|
||||
bunnies[bunniesCount].color_b = random.randint(100, 240)
|
||||
bunnies[bunniesCount].color_a = 255
|
||||
bunniesCount+=1
|
||||
|
||||
|
||||
# // Update bunnies
|
||||
for i in range(0, bunniesCount):
|
||||
bunnies[i].position_x += bunnies[i].speed_x
|
||||
bunnies[i].position_y += bunnies[i].speed_y
|
||||
|
||||
if ((bunnies[i].position_x + texBunny.width/2) > GetScreenWidth()) or ((bunnies[i].position_x + texBunny.width/2) < 0):
|
||||
bunnies[i].speed_x *= -1
|
||||
if ((bunnies[i].position_y + texBunny.height/2) > GetScreenHeight()) or ((bunnies[i].position_y + texBunny.height/2 - 40) < 0):
|
||||
bunnies[i].speed_y *= -1
|
||||
|
||||
# //----------------------------------------------------------------------------------
|
||||
#
|
||||
# // Draw
|
||||
# //----------------------------------------------------------------------------------
|
||||
BeginDrawing()
|
||||
|
||||
ClearBackground(RAYWHITE)
|
||||
|
||||
for i in range(0, bunniesCount):
|
||||
# // NOTE: When internal batch buffer limit is reached (MAX_BATCH_ELEMENTS),
|
||||
# // a draw call is launched and buffer starts being filled again;
|
||||
# // before issuing a draw call, updated vertex data from internal CPU buffer is send to GPU...
|
||||
# // Process of sending data is costly and it could happen that GPU data has not been completely
|
||||
# // processed for drawing while new data is tried to be sent (updating current in-use buffers)
|
||||
# // it could generates a stall and consequently a frame drop, limiting the number of drawn bunnies
|
||||
DrawTexture(texBunny, int(bunnies[i].position_x), int(bunnies[i].position_y), (bunnies[i].color_r,bunnies[i].color_g,bunnies[i].color_b,bunnies[i].color_a))
|
||||
|
||||
DrawRectangle(0, 0, screenWidth, 40, BLACK)
|
||||
text = f"bunnies {bunniesCount}"
|
||||
DrawText(text.encode('utf-8'), 120, 10, 20, GREEN)
|
||||
text = f"batched draw calls: { 1 + int(bunniesCount/MAX_BATCH_ELEMENTS)}"
|
||||
DrawText(text.encode('utf-8'), 320, 10, 20, MAROON)
|
||||
|
||||
DrawFPS(10, 10)
|
||||
|
||||
EndDrawing()
|
||||
#//----------------------------------------------------------------------------------
|
||||
|
||||
|
||||
#// De-Initialization
|
||||
#//--------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
UnloadTexture(texBunny); #Unload bunny texture
|
||||
|
||||
CloseWindow() # Close window and OpenGL context
|
||||
#//--------------------------------------------------------------------------------------
|
||||
|
Reference in a new issue