Skip to content

Instantly share code, notes, and snippets.

@AbePralle
Created December 22, 2020 08:52
Show Gist options
  • Select an option

  • Save AbePralle/f58646d0b0ddf479e1eee7598fe734e8 to your computer and use it in GitHub Desktop.

Select an option

Save AbePralle/f58646d0b0ddf479e1eee7598fe734e8 to your computer and use it in GitHub Desktop.
Testing has_another/read versus read_next
uses Bitmap
U5MapGen()
class U5MapGen
PROPERTIES
chunk_map = ...
"~~#############~" ...
"~##############~" ...
"###############~" ...
"################" ...
"################" ...
"################" ...
"###########~####" ...
"#######~~~##~###" ...
"~######~~~##~###" ...
"#######~#~~~~##~" ...
"#######~#~~##~~~" ...
"~~######~~~~~~##" ...
"~##~####~~##~###" ...
"~##~####~~##~###" ...
"~###############" ...
"~~~##~##########"
METHODS
method init
local bitmap = Bitmap( 256*16, 256*16 )
local tiles = Bitmap( File("Assets/U5Tiles.png") ).split( 32, 16 )
local reader = File( "Assets/BRIT.DAT" ).reader
local chunk_type_reader = chunk_map.reader
forEach (chunk_j in 0..15)
forEach (chunk_i in 0..15)
if (chunk_type_reader.read == '~')
# All water
forEach (j in 0..15)
forEach (i in 0..15)
tiles[ 1 ].blit( bitmap, (chunk_i*16+i)*16, (chunk_j*16+j)*16 )
endForEach
endForEach
else
forEach (j in 0..15)
forEach (i in 0..15)
tiles[ reader.read&511 ].blit( bitmap, (chunk_i*16+i)*16, (chunk_j*16+j)*16 )
endForEach
endForEach
endIf
endForEach
endForEach
test_has_another( bitmap )
test_read_next( bitmap )
test_has_another( bitmap )
test_read_next( bitmap )
bitmap.save_as_png( File("U5Map.png") )
System.run( "open U5Map.png" )
method test_has_another( bitmap:Bitmap )
println "Testing black pixels using has_another/read"
local n = 0
local stopwatch = Stopwatch()
forEach (color in PixelIterator(bitmap))
if (color.argb == 0xFF000000) ++n
endForEach
@trace n, stopwatch
println
method test_read_next( bitmap:Bitmap )
println "Testing black pixels using read_next"
local n = 0
local stopwatch = Stopwatch()
local iterator = PixelIterator( bitmap )
local optional_color = iterator.read_next
while (optional_color.exists)
local color = optional_color.value
optional_color = iterator.read_next
if (color.argb == 0xFF000000) ++n
endWhile
@trace n, stopwatch
println
endClass
class PixelIterator( pixels:Color[], position:Int32, limit:Int32 ) [compound]
GLOBAL METHODS
method create( bitmap:Bitmap )->PixelIterator
return PixelIterator( bitmap.pixels, 0, bitmap.pixels.count )
METHODS
method has_another->Logical
return (position < limit)
method read->Color [mutating]
++position
return pixels[ position-1 ]
method read_next->Color? [mutating]
if (position == limit) return null
++position
return pixels[ position-1 ]
endClass
# RESULTS
# -------------------------------------------
# -O0
# -------------------------------------------
# Testing black pixels using has_another/read
# n:12328534 stopwatch:0.41 seconds
#
# Testing black pixels using read_next
# n:12328534 stopwatch:0.21 seconds
#
# Testing black pixels using has_another/read
# n:12328534 stopwatch:0.42 seconds
#
# Testing black pixels using read_next
# n:12328534 stopwatch:0.20 seconds
#
# -------------------------------------------
# -O3
# -------------------------------------------
# Testing black pixels using has_another/read
# n:12328534 stopwatch:0.02 seconds
#
# Testing black pixels using read_next
# n:12328534 stopwatch:0.01 seconds
#
# Testing black pixels using has_another/read
# n:12328534 stopwatch:0.02 seconds
#
# Testing black pixels using read_next
# n:12328534 stopwatch:0.01 seconds
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment