diff --git a/.gitignore b/.gitignore index 441631a..b23eea2 100644 --- a/.gitignore +++ b/.gitignore @@ -3,5 +3,6 @@ deps/downloads deps/usr deps/src deps/deps.jl +test/test_results .DS_Store *~ diff --git a/test/shape_functions.jl b/test/shape_functions.jl index 2286b5d..a2d46a7 100644 --- a/test/shape_functions.jl +++ b/test/shape_functions.jl @@ -4,195 +4,195 @@ using Cairo function randpos(n,w::Real,h::Real) - srand(141413); - px = rand(n)*w; - py = rand(n)*h; - return (px,py); + srand(141413) + px = rand(n)*w + py = rand(n)*h + return (px,py) end function clear_bg(c::CairoContext,w::Real,h::Real) - save(c); - set_source_rgb(c,1.0,1.0,1.0); - rectangle(c, 0,0,w,h); - paint(c); - restore(c); + save(c) + set_source_rgb(c,1.0,1.0,1.0) + rectangle(c, 0,0,w,h) + paint(c) + restore(c) end # ddotsx: fill random dots with different methods, disc only (no ring) function ddots1(cr::CairoContext, rect_width::Real, rect_height::Real, radius::Real, n::Int) - clear_bg(cr,rect_width,rect_height); - px,py = randpos(n,rect_width,rect_height); + clear_bg(cr,rect_width,rect_height) + px,py = randpos(n,rect_width,rect_height) - new_path(cr); + new_path(cr) for i=1:n - move_to(cr,px[i],py[i]); - rel_move_to(cr,radius,0); - arc(cr, px[i], py[i], radius, 0, 2*pi); + move_to(cr,px[i],py[i]) + rel_move_to(cr,radius,0) + arc(cr, px[i], py[i], radius, 0, 2*pi) end - close_path(cr); + close_path(cr) - set_source_rgb(cr, 0, 0, 1.0); - fill(cr); + set_source_rgb(cr, 0, 0, 1.0) + fill(cr) end function ddots2(cr::CairoContext, rect_width::Real, rect_height::Real, radius::Real, n::Int) - clear_bg(cr,rect_width,rect_height); - px,py = randpos(n,rect_width,rect_height); + clear_bg(cr,rect_width,rect_height) + px,py = randpos(n,rect_width,rect_height) for i=1:n - new_path(cr); - move_to(cr,px[i],py[i]); - rel_move_to(cr,radius,0); - arc(cr, px[i], py[i], radius, 0, 2*pi); - close_path(cr); - set_source_rgb(cr, 0, 0, 1.0); - fill(cr); + new_path(cr) + move_to(cr,px[i],py[i]) + rel_move_to(cr,radius,0) + arc(cr, px[i], py[i], radius, 0, 2*pi) + close_path(cr) + set_source_rgb(cr, 0, 0, 1.0) + fill(cr) end end function ddots3(cr::CairoContext, rect_width::Real, rect_height::Real, radius::Real, n::Int) - clear_bg(cr,rect_width,rect_height); - px,py = randpos(n,rect_width,rect_height); + clear_bg(cr,rect_width,rect_height) + px,py = randpos(n,rect_width,rect_height) - new_path(cr); + new_path(cr) - set_source_rgb(cr, 0, 0, 1.0); + set_source_rgb(cr, 0, 0, 1.0) set_line_cap(cr,Cairo.CAIRO_LINE_CAP_ROUND) set_line_width(cr,radius*2.0) for i=1:n - move_to(cr,px[i],py[i]); - rel_line_to(cr,0,0); - stroke(cr); + move_to(cr,px[i],py[i]) + rel_line_to(cr,0,0) + stroke(cr) end - close_path (cr); + close_path (cr) end function ddots4(cr::CairoContext, rect_width::Real, rect_height::Real, radius::Real, n::Int) - clear_bg(cr,rect_width,rect_height); - px,py = randpos(n,rect_width,rect_height); + clear_bg(cr,rect_width,rect_height) + px,py = randpos(n,rect_width,rect_height) cc = radius + 1 - rectangle(cr,0,0,2*cc,2*cc); - clip(cr); - push_group(cr); - arc(cr,radius,radius,radius,0,2*pi); - set_source_rgb(cr,0,0,1.0); - fill(cr); - p = pop_group(cr); - reset_clip(cr); + rectangle(cr,0,0,2*cc,2*cc) + clip(cr) + push_group(cr) + arc(cr,radius,radius,radius,0,2*pi) + set_source_rgb(cr,0,0,1.0) + fill(cr) + p = pop_group(cr) + reset_clip(cr) for i=1:n - save(cr); - translate(cr,px[i]-cc,py[i]-cc); - set_source(cr,p); - paint(cr); - restore(cr); + save(cr) + translate(cr,px[i]-cc,py[i]-cc) + set_source(cr,p) + paint(cr) + restore(cr) end end function ddots5(cr::CairoContext, rect_width::Real, rect_height::Real, radius::Real, n::Int) - clear_bg(cr,rect_width,rect_height); - px,py = randpos(n,rect_width,rect_height); - - cc = radius + 1; - - s1 = Cairo.CairoARGBSurface(cc*2,cc*2); - c1 = Cairo.CairoContext(s1); - rectangle(c1,0,0,2*cc,2*cc); - set_source_rgba(c1,0,0,0,0); - paint(c1); - new_path(c1); - arc(c1,radius,radius,radius,0,2*pi); - set_source_rgb(c1,0,0,1.0); - fill(c1); - - p = Cairo.CairoPattern(s1); + clear_bg(cr,rect_width,rect_height) + px,py = randpos(n,rect_width,rect_height) + + cc = radius + 1 + + s1 = Cairo.CairoARGBSurface(cc*2,cc*2) + c1 = Cairo.CairoContext(s1) + rectangle(c1,0,0,2*cc,2*cc) + set_source_rgba(c1,0,0,0,0) + paint(c1) + new_path(c1) + arc(c1,radius,radius,radius,0,2*pi) + set_source_rgb(c1,0,0,1.0) + fill(c1) + + p = Cairo.CairoPattern(s1) for i=1:n - save(cr); - translate(cr,px[i]-cc,py[i]-cc); - set_source(cr,p); - paint(cr); - restore(cr); + save(cr) + translate(cr,px[i]-cc,py[i]-cc) + set_source(cr,p) + paint(cr) + restore(cr) end end # rdotsx: fill random dots with different methods function rdots1(cr::CairoContext, rect_width::Real, rect_height::Real, radius::Real, n::Int) - clear_bg(cr,rect_width,rect_height); - px,py = randpos(n,rect_width,rect_height); + clear_bg(cr,rect_width,rect_height) + px,py = randpos(n,rect_width,rect_height) - new_path(cr); + new_path(cr) for i=1:n - move_to(cr,px[i],py[i]); - rel_move_to(cr,radius,0); - arc(cr, px[i], py[i], radius, 0, 2*pi); + move_to(cr,px[i],py[i]) + rel_move_to(cr,radius,0) + arc(cr, px[i], py[i], radius, 0, 2*pi) end - close_path(cr); + close_path(cr) - set_source_rgb(cr, 0, 0, 1.0); - fill_preserve(cr); - set_line_width(cr,1.0); - set_source_rgb(cr, 0, 0, 0); - stroke(cr); + set_source_rgb(cr, 0, 0, 1.0) + fill_preserve(cr) + set_line_width(cr,1.0) + set_source_rgb(cr, 0, 0, 0) + stroke(cr) end function rdots2(cr::CairoContext, rect_width::Real, rect_height::Real, radius::Real, n::Int) - clear_bg(cr,rect_width,rect_height); - px,py = randpos(n,rect_width,rect_height); + clear_bg(cr,rect_width,rect_height) + px,py = randpos(n,rect_width,rect_height) for i=1:n - new_path(cr); - move_to(cr,px[i],py[i]); - rel_move_to(cr,radius,0); - arc(cr, px[i], py[i], radius, 0, 2*pi); - close_path(cr); - set_source_rgb(cr, 0, 0, 1.0); - fill_preserve(cr); - set_line_width(cr,1.0); - set_source_rgb(cr, 0, 0, 0); - stroke(cr); + new_path(cr) + move_to(cr,px[i],py[i]) + rel_move_to(cr,radius,0) + arc(cr, px[i], py[i], radius, 0, 2*pi) + close_path(cr) + set_source_rgb(cr, 0, 0, 1.0) + fill_preserve(cr) + set_line_width(cr,1.0) + set_source_rgb(cr, 0, 0, 0) + stroke(cr) end end function rdots3(cr::CairoContext, rect_width::Real, rect_height::Real, radius::Real, n::Int) - clear_bg(cr,rect_width,rect_height); - px,py = randpos(n,rect_width,rect_height); + clear_bg(cr,rect_width,rect_height) + px,py = randpos(n,rect_width,rect_height) - new_path(cr); + new_path(cr) - set_line_cap(cr,Cairo.CAIRO_LINE_CAP_ROUND); + set_line_cap(cr,Cairo.CAIRO_LINE_CAP_ROUND) - set_source_rgb(cr, 0, 0, 0); - set_line_width(cr,radius*2 + 2.0); + set_source_rgb(cr, 0, 0, 0) + set_line_width(cr,radius*2 + 2.0) for i=1:n - move_to(cr,px[i],py[i]); - rel_line_to(cr,0,0); - stroke(cr); + move_to(cr,px[i],py[i]) + rel_line_to(cr,0,0) + stroke(cr) end - set_source_rgb(cr, 0, 0, 1.0); - set_line_width(cr,radius*2); + set_source_rgb(cr, 0, 0, 1.0) + set_line_width(cr,radius*2) for i=1:n - move_to(cr,px[i],py[i]); - rel_line_to(cr,0,0); - stroke(cr); + move_to(cr,px[i],py[i]) + rel_line_to(cr,0,0) + stroke(cr) end @@ -201,61 +201,61 @@ end function rdots4(cr::CairoContext, rect_width::Real, rect_height::Real, radius::Real, n::Int) - clear_bg(cr,rect_width,rect_height); - px,py = randpos(n,rect_width,rect_height); - - cc = radius + 3.0; - - rectangle(cr,0,0,2*cc,2*cc); - clip(cr); - push_group(cr); - arc(cr,radius,radius,radius,0,2*pi); - set_source_rgb(cr,0,0,1.0); - fill_preserve(cr); - set_source_rgb(cr,0,0,0); - set_line_width(cr,1.0); - stroke(cr); - p = pop_group(cr); - reset_clip(cr); + clear_bg(cr,rect_width,rect_height) + px,py = randpos(n,rect_width,rect_height) + + cc = radius + 3.0 + + rectangle(cr,0,0,2*cc,2*cc) + clip(cr) + push_group(cr) + arc(cr,radius,radius,radius,0,2*pi) + set_source_rgb(cr,0,0,1.0) + fill_preserve(cr) + set_source_rgb(cr,0,0,0) + set_line_width(cr,1.0) + stroke(cr) + p = pop_group(cr) + reset_clip(cr) for i=1:n - save(cr); - translate(cr,px[i]-cc,py[i]-cc); - set_source(cr,p); - paint(cr); - restore(cr); + save(cr) + translate(cr,px[i]-cc,py[i]-cc) + set_source(cr,p) + paint(cr) + restore(cr) end end function rdots5(cr::CairoContext, rect_width::Real, rect_height::Real, radius::Real, n::Int) - clear_bg(cr,rect_width,rect_height); - px,py = randpos(n,rect_width,rect_height); - - cc = radius + 3.0; - - s1 = Cairo.CairoARGBSurface(cc*2,cc*2); - c1 = Cairo.CairoContext(s1); - rectangle(c1,0,0,2*cc,2*cc); - set_source_rgba(c1,0,0,0,0); - paint(c1); - new_path(c1); - arc(c1,radius+1,radius+1,radius,0,2*pi); - set_source_rgb(c1,0,0,1.0); - fill_preserve(c1); - set_source_rgb(c1,0,0,0); - set_line_width(c1,1.0); - stroke(c1); - - p = Cairo.CairoPattern(s1); + clear_bg(cr,rect_width,rect_height) + px,py = randpos(n,rect_width,rect_height) + + cc = radius + 3.0 + + s1 = Cairo.CairoARGBSurface(cc*2,cc*2) + c1 = Cairo.CairoContext(s1) + rectangle(c1,0,0,2*cc,2*cc) + set_source_rgba(c1,0,0,0,0) + paint(c1) + new_path(c1) + arc(c1,radius+1,radius+1,radius,0,2*pi) + set_source_rgb(c1,0,0,1.0) + fill_preserve(c1) + set_source_rgb(c1,0,0,0) + set_line_width(c1,1.0) + stroke(c1) + + p = Cairo.CairoPattern(s1) for i=1:n - save(cr); - translate(cr,px[i]-cc,py[i]-cc); - set_source(cr,p); - paint(cr); - restore(cr); + save(cr) + translate(cr,px[i]-cc,py[i]-cc) + set_source(cr,p) + paint(cr) + restore(cr) end end @@ -263,96 +263,96 @@ end # lines0, random x,y lines function lines0(cr::CairoContext, rect_width::Real, rect_height::Real, width::Real, n::Int) - clear_bg(cr,rect_width,rect_height); - px,py = randpos(n,rect_width,rect_height); + clear_bg(cr,rect_width,rect_height) + px,py = randpos(n,rect_width,rect_height) - new_path(cr); + new_path(cr) - set_source_rgb(cr, 0, 0, 1.0); + set_source_rgb(cr, 0, 0, 1.0) set_line_width(cr,width) - new_path(cr); - move_to(cr,px[1],py[1]); + new_path(cr) + move_to(cr,px[1],py[1]) for i=2:n - line_to(cr,px[i],py[i]); + line_to(cr,px[i],py[i]) end - stroke(cr); + stroke(cr) end # lines1, x sorted, line a plot function lines1(cr::CairoContext, rect_width::Real, rect_height::Real, width::Real, n::Int) - clear_bg(cr,rect_width,rect_height); - px,py = randpos(n,rect_width,rect_height); + clear_bg(cr,rect_width,rect_height) + px,py = randpos(n,rect_width,rect_height) - px = sort(px); + px = sort(px) - set_source_rgb(cr, 0, 0, 1.0); + set_source_rgb(cr, 0, 0, 1.0) set_line_width(cr,width) - new_path(cr); - move_to(cr,px[1],py[1]); + new_path(cr) + move_to(cr,px[1],py[1]) for i=2:n - line_to(cr,px[i],py[i]); + line_to(cr,px[i],py[i]) end - stroke(cr); + stroke(cr) end # lines2, x sorted, independent lines per coord function lines2(cr::CairoContext, rect_width::Real, rect_height::Real, width::Real, n::Int) - clear_bg(cr,rect_width,rect_height); - px,py = randpos(n,rect_width,rect_height); + clear_bg(cr,rect_width,rect_height) + px,py = randpos(n,rect_width,rect_height) - px = sort(px); + px = sort(px) - set_source_rgb(cr, 0, 0, 1.0); + set_source_rgb(cr, 0, 0, 1.0) set_line_width(cr,width) - new_path(cr); + new_path(cr) for i=1:n-1 - move_to(cr,px[i],py[i]); - line_to(cr,px[i+1],py[i+1]); - stroke(cr); + move_to(cr,px[i],py[i]) + line_to(cr,px[i+1],py[i+1]) + stroke(cr) end end # lines3, x sorted, in clusters of 100 function lines3(cr::CairoContext, rect_width::Real, rect_height::Real, width::Real, n::Int) - clear_bg(cr,rect_width,rect_height); - px,py = randpos(n,rect_width,rect_height); + clear_bg(cr,rect_width,rect_height) + px,py = randpos(n,rect_width,rect_height) - px = sort(px); + px = sort(px) - set_source_rgb(cr, 0, 0, 1.0); - set_line_width(cr,width); - new_path(cr); + set_source_rgb(cr, 0, 0, 1.0) + set_line_width(cr,width) + new_path(cr) for c = 1:100:n - move_to(cr,px[c],py[c]); - for i=c:min(c+100,n) - line_to(cr,px[i],py[i]); + move_to(cr,px[c],py[c]) + for i=c:min(c+100,n) + line_to(cr,px[i],py[i]) end - stroke(cr); + stroke(cr) end end # lines4, x sorted, in clusters of 1000 function lines4(cr::CairoContext, rect_width::Real, rect_height::Real, width::Real, n::Int) - clear_bg(cr,rect_width,rect_height); - px,py = randpos(n,rect_width,rect_height); + clear_bg(cr,rect_width,rect_height) + px,py = randpos(n,rect_width,rect_height) - px = sort(px); + px = sort(px) - set_source_rgb(cr, 0, 0, 1.0); - set_line_width(cr,width); - new_path(cr); + set_source_rgb(cr, 0, 0, 1.0) + set_line_width(cr,width) + new_path(cr) for c = 1:1000:n - move_to(cr,px[c],py[c]); - for i=c:min(c+1000,n) - line_to(cr,px[i],py[i]); + move_to(cr,px[c],py[c]) + for i=c:min(c+1000,n) + line_to(cr,px[i],py[i]) end - stroke(cr); + stroke(cr) end end diff --git a/test/test_speed.jl b/test/test_speed.jl index 740c2b8..5c6ae75 100644 --- a/test/test_speed.jl +++ b/test/test_speed.jl @@ -1,69 +1,53 @@ using Cairo -import shape_functions - -#function test_all() - - print("cairo version: ",ccall((:cairo_version,Cairo._jl_libcairo),Int32,()),"\n"); - - size_surface = [512];#,512,1024]; #three sizes of a surface - - paint_width = [0.5,1.0,3.0,5.0]; - - shapes = [ddots1, ddots2, ddots3, ddots4, ddots5, - rdots1, rdots2, rdots3, rdots4, rdots5, - lines0, lines1, lines2, lines3, lines4]; - - n_elements = [100,300,1000,3000,10000,30000,100000]; - - n_tests = length(size_surface) * length(paint_width) * length(shapes); - test_data = zeros(n_tests+1,3 + length(n_elements)); - - test_data[1,3+(1:length(n_elements))] = n_elements; - - save_flag = false; - - i = 1; - f = 1; - - for s_size in size_surface - - s = Cairo.CairoARGBSurface(s_size,s_size); - c = Cairo.CairoContext(s); - - print("Surface Size: ",s_size,"\n"); - - for w in paint_width - - print("Paint Width: ",w,"\n"); - - for m_index in 1:length(shapes) - - test_data[i,1] = s_size; - test_data[i,2] = w; - test_data[i,3] = m_index; - m = shapes[m_index]; - - for n_index in 1:length(n_elements) - - n = n_elements[n_index]; - t = @elapsed(m(c,s_size,s_size,w,n)); - print(m," ",@sprintf("%6d",n)," elapsed time: ",@sprintf("%f seconds",t),"\n"); - test_data[i+1,3+n_index] = t; - - if save_flag - write_to_png(s,@sprintf("test_speed_%06d.png",f)); - end - f += 1; - - end - print("\n"); - i += 1; - - end - end - end - - #test_data; -#end; - -#print(test_all); \ No newline at end of file +include("shape_functions.jl") + +function test_all(;tmax = 2.0, save_flag = false) + print("cairo version: ",ccall((:cairo_version,Cairo._jl_libcairo),Int32,()),"\n"); + size_surface = [512];#,512,1024]; #three sizes of a surface + paint_width = [0.5,1.0,3.0,5.0]; + shapes = [ddots1, ddots2, ddots3, ddots4, ddots5, + rdots1, rdots2, rdots3, rdots4, rdots5, + lines0, lines1, lines2, lines3, lines4]; + n_elements = [100,300,1000,3000,10000,30000,100000]; + + n_tests = length(size_surface) * length(paint_width) * length(shapes); + test_data = zeros(n_tests+1,3 + length(n_elements)); + test_data[1,3+(1:length(n_elements))] = n_elements; + i = 1; + + if save_flag && !isdir("test_results") + mkdir("test_results") + end + + for s_size in size_surface + s = Cairo.CairoARGBSurface(s_size,s_size); + c = Cairo.CairoContext(s); + print("Surface Size: ",s_size,"\n") + for w in paint_width + print("Paint Width: ",w,"\n") + for m_index in 1:length(shapes) + test_data[i,1] = s_size; + test_data[i,2] = w; + test_data[i,3] = m_index; + m = shapes[m_index]; + for n_index in 1:length(n_elements) + n = n_elements[n_index]; + t = @elapsed(m(c,s_size,s_size,w,n)); + print(m," ",@sprintf("%6d",n)," elapsed time: ",@sprintf("%f seconds",t),"\n"); + test_data[i+1,3+n_index] = t; + if save_flag + write_to_png(s,@sprintf("test_results/test_speed_%2.1f_%02d_%02d.png",w,m_index,n_index)); + end + if t > tmax && n_index < length(n_elements) + println("Remaining tests in this sequence skipped") + break + end + end + print("\n") + i += 1; + end + end + end +end + +test_all()