1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Some benchmarks for measuring graphics operations performance
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2008 Vadim Zeitlin <vadim@wxwidgets.org>
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
13 #include "wx/cmdline.h"
14 #include "wx/dcclient.h"
15 #include "wx/dcmemory.h"
16 #include "wx/dcgraph.h"
17 #include "wx/stopwatch.h"
19 struct GraphicsBenchmarkOptions
21 GraphicsBenchmarkOptions()
39 class GraphicsBenchmarkFrame
: public wxFrame
42 GraphicsBenchmarkFrame()
43 : wxFrame(NULL
, wxID_ANY
, "wxWidgets Graphics Benchmark")
46 wxPaintEventHandler(GraphicsBenchmarkFrame
::OnPaint
));
48 m_bitmap
.Create(64, 64, 32);
51 SetClientSize(opts
.width
, opts
.height
);
54 BenchmarkLines("dc client", dc
);
55 BenchmarkRectangles("dc client", dc
);
56 BenchmarkBitmaps("dc client", dc
);
59 BenchmarkLines("gcdc client", gcdc
);
60 BenchmarkRectangles("gcdc client", gcdc
);
61 BenchmarkBitmaps("gcdc client", gcdc
);
63 wxBitmap
bmp(opts
.width
, opts
.height
);
65 BenchmarkLines("dc memory", dc2
);
66 BenchmarkRectangles("dc memory", dc2
);
67 BenchmarkBitmaps("dc memory", dc2
);
70 BenchmarkLines("gcdc memory", gcdc2
);
71 BenchmarkRectangles("gcdc memory", gcdc2
);
72 BenchmarkBitmaps("gcdc memory", gcdc2
);
76 void OnPaint(wxPaintEvent
& WXUNUSED(event
))
79 BenchmarkLines("dc paint", dc
);
80 BenchmarkRectangles("dc paint", dc
);
81 BenchmarkBitmaps("dc paint", dc
);
84 BenchmarkLines("gcdc paint", gcdc
);
85 BenchmarkRectangles("gcdc paint", gcdc
);
86 BenchmarkBitmaps("gcdc paint", gcdc
);
88 wxTheApp
->ExitMainLoop();
92 void BenchmarkLines(const char *msg
, wxDC
& dc
)
94 if ( opts
.mapMode
!= 0 )
95 dc
.SetMapMode((wxMappingMode
)opts
.mapMode
);
96 if ( opts
.penWidth
!= 0 )
97 dc
.SetPen(wxPen(*wxWHITE
, opts
.penWidth
));
99 wxPrintf("Benchmarking %s DC: ", msg
);
104 for ( int n
= 0; n
< opts
.numLines
; n
++ )
106 int x1
= rand() % opts
.width
,
107 y1
= rand() % opts
.height
;
109 dc
.DrawLine(x
, y
, x1
, y1
);
115 const long t
= sw
.Time();
117 wxPrintf("%d lines done in %lums = %gus/line\n",
118 opts
.numLines
, t
, (1000. * t
)/opts
.numLines
);
122 void BenchmarkRectangles(const char *msg
, wxDC
& dc
)
124 if ( opts
.mapMode
!= 0 )
125 dc
.SetMapMode((wxMappingMode
)opts
.mapMode
);
126 if ( opts
.penWidth
!= 0 )
127 dc
.SetPen(wxPen(*wxWHITE
, opts
.penWidth
));
129 dc
.SetBrush( *wxRED_BRUSH
);
131 wxPrintf("Benchmarking %s DC: ", msg
);
134 for ( int n
= 0; n
< opts
.numLines
; n
++ )
136 int x
= rand() % opts
.width
,
137 y
= rand() % opts
.height
;
139 dc
.DrawRectangle(x
, y
, 32, 32);
142 const long t
= sw
.Time();
144 wxPrintf("%d rects done in %lums = %gus/rect\n",
145 opts
.numLines
, t
, (1000. * t
)/opts
.numLines
);
148 void BenchmarkBitmaps(const char *msg
, wxDC
& dc
)
150 if ( opts
.mapMode
!= 0 )
151 dc
.SetMapMode((wxMappingMode
)opts
.mapMode
);
152 if ( opts
.penWidth
!= 0 )
153 dc
.SetPen(wxPen(*wxWHITE
, opts
.penWidth
));
155 wxPrintf("Benchmarking %s DC: ", msg
);
158 for ( int n
= 0; n
< opts
.numLines
; n
++ )
160 int x
= rand() % opts
.width
,
161 y
= rand() % opts
.height
;
163 dc
.DrawBitmap(m_bitmap
, x
, y
, true);
166 const long t
= sw
.Time();
168 wxPrintf("%d bitmaps done in %lums = %gus/bitmap\n",
169 opts
.numLines
, t
, (1000. * t
)/opts
.numLines
);
176 class GraphicsBenchmarkApp
: public wxApp
179 virtual void OnInitCmdLine(wxCmdLineParser
& parser
)
181 static const wxCmdLineEntryDesc desc
[] =
183 { wxCMD_LINE_OPTION
, "m", "map-mode", "", wxCMD_LINE_VAL_NUMBER
},
184 { wxCMD_LINE_OPTION
, "p", "pen-width", "", wxCMD_LINE_VAL_NUMBER
},
185 { wxCMD_LINE_OPTION
, "w", "width", "", wxCMD_LINE_VAL_NUMBER
},
186 { wxCMD_LINE_OPTION
, "h", "height", "", wxCMD_LINE_VAL_NUMBER
},
187 { wxCMD_LINE_OPTION
, "L", "lines", "", wxCMD_LINE_VAL_NUMBER
},
191 parser
.SetDesc(desc
);
194 virtual bool OnCmdLineParsed(wxCmdLineParser
& parser
)
196 if ( parser
.Found("m", &opts
.mapMode
) &&
197 (opts
.mapMode
< 1 || opts
.mapMode
> wxMM_METRIC
) )
199 if ( parser
.Found("p", &opts
.penWidth
) && opts
.penWidth
< 1 )
201 if ( parser
.Found("w", &opts
.width
) && opts
.width
< 1 )
203 if ( parser
.Found("h", &opts
.height
) && opts
.height
< 1 )
205 if ( parser
.Found("L", &opts
.numLines
) && opts
.numLines
< 1 )
211 virtual bool OnInit()
213 if ( !wxApp
::OnInit() )
216 new GraphicsBenchmarkFrame
;
222 IMPLEMENT_APP_CONSOLE(GraphicsBenchmarkApp
)