The Benchmark module allows you to write easily benchmarks framework to a project for timing critical parts and detecting slow parts of code.

In addition it automatically creates data files of these benchmarks, as well as a gnuplot file which can display the comparison curves of the benchmarks.\hypertarget{tutorial_benchmark_page_tutorial_benchmark_basic_usage}{}\doxysection{Basic Usage}\label{tutorial_benchmark_page_tutorial_benchmark_basic_usage}
To create a basic benchmark, you have to follow these steps\+:

\begin{DoxyItemize}
\item Create a new benchmark \item Write the functions that wraps the functions you want to benchmark. \item Register these wrappers functions. \item Run the benchmark. \item Free the memory.\end{DoxyItemize}
Here is a basic example of benchmark which creates two functions that will be run. These functions just print a message.


\begin{DoxyCode}{0}
\DoxyCodeLine{\textcolor{preprocessor}{\#include\ <stdlib.h>}}
\DoxyCodeLine{\textcolor{preprocessor}{\#include\ <stdio.h>}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{preprocessor}{\#include\ <\mbox{\hyperlink{Eina_8h}{Eina.h}}>}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{static}}
\DoxyCodeLine{\textcolor{keywordtype}{void}\ work1(\textcolor{keywordtype}{int}\ request)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ printf\ (\textcolor{stringliteral}{"{}work1\ in\ progress...\ Request:\ \%d\(\backslash\)n"{}},\ request);}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{static}}
\DoxyCodeLine{\textcolor{keywordtype}{void}\ work2(\textcolor{keywordtype}{int}\ request)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ printf\ (\textcolor{stringliteral}{"{}work2\ in\ progress...\ Request:\ \%d\(\backslash\)n"{}},\ request);}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keywordtype}{int}\ main()}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \mbox{\hyperlink{group__Eina__Benchmark__Group_gac5c05887f0ad72bfb28b53e6fde49ce2}{Eina\_Benchmark}}\ *test;}
\DoxyCodeLine{\ \ \mbox{\hyperlink{struct__Eina__Array}{Eina\_Array}}\ \ \ \ \ *ea;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \textcolor{keywordflow}{if}\ (!\mbox{\hyperlink{group__Eina__Main__Group_ga81b8137665b8a4d2cb38ca50017d4c74}{eina\_init}}())}
\DoxyCodeLine{\ \ \ \ \textcolor{keywordflow}{return}\ EXIT\_FAILURE;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ test\ =\ \mbox{\hyperlink{group__Eina__Benchmark__Group_ga0553fada9227bc8b5b9f9c8bfed9def7}{eina\_benchmark\_new}}(\textcolor{stringliteral}{"{}test"{}},\ \textcolor{stringliteral}{"{}run"{}});}
\DoxyCodeLine{\ \ \textcolor{keywordflow}{if}\ (!test)}
\DoxyCodeLine{\ \ \ \ \textcolor{keywordflow}{goto}\ shutdown\_eina;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \mbox{\hyperlink{group__Eina__Benchmark__Group_ga9732a7da5665e92d0afaf0367d2f6b85}{eina\_benchmark\_register}}(test,\ \textcolor{stringliteral}{"{}work-\/1"{}},\ \mbox{\hyperlink{group__Eina__Benchmark__Group_ga64c3594df1ad868efe1454c34e3e1c37}{EINA\_BENCHMARK}}(work1),\ 200,\ 300,\ 10);}
\DoxyCodeLine{\ \ \mbox{\hyperlink{group__Eina__Benchmark__Group_ga9732a7da5665e92d0afaf0367d2f6b85}{eina\_benchmark\_register}}(test,\ \textcolor{stringliteral}{"{}work-\/2"{}},\ \mbox{\hyperlink{group__Eina__Benchmark__Group_ga64c3594df1ad868efe1454c34e3e1c37}{EINA\_BENCHMARK}}(work2),\ 100,\ 150,\ 5);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ ea\ =\ \mbox{\hyperlink{group__Eina__Benchmark__Group_ga0375144e67c0f4ca6f687236bc845cfe}{eina\_benchmark\_run}}(test);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \mbox{\hyperlink{group__Eina__Benchmark__Group_ga07f1276628b40aac723f21c75b5d5b76}{eina\_benchmark\_free}}(test);}
\DoxyCodeLine{\ \ \mbox{\hyperlink{group__Eina__Main__Group_ga5bf35513199d5220341437c05696285d}{eina\_shutdown}}();}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \textcolor{keywordflow}{return}\ EXIT\_SUCCESS;}
\DoxyCodeLine{}
\DoxyCodeLine{\ shutdown\_eina:}
\DoxyCodeLine{\ \ \mbox{\hyperlink{group__Eina__Main__Group_ga5bf35513199d5220341437c05696285d}{eina\_shutdown}}();}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \textcolor{keywordflow}{return}\ EXIT\_FAILURE;}
\DoxyCodeLine{\}}

\end{DoxyCode}


As \char`\"{}test\char`\"{}, \char`\"{}run\char`\"{} are passed to \mbox{\hyperlink{group__Eina__Benchmark__Group_ga0553fada9227bc8b5b9f9c8bfed9def7}{eina\+\_\+benchmark\+\_\+new()}} and as the tests \char`\"{}work-\/1\char`\"{} and \char`\"{}work-\/2\char`\"{} are registered, the data files bench\+\_\+test\+\_\+run.\+work-\/1.\+data and bench\+\_\+test\+\_\+run.\+work-\/2.\+data will be created after the \mbox{\hyperlink{group__Eina__Benchmark__Group_ga0375144e67c0f4ca6f687236bc845cfe}{eina\+\_\+benchmark\+\_\+run()}} call. They contain four columns. The file bench\+\_\+test\+\_\+run.\+work-\/1.\+data contains for example\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{\textcolor{preprocessor}{\#\ specimen\ \ \ \ \ \ experiment\ time\ starting\ time\ \ \ ending\ time}}
\DoxyCodeLine{200\ \ \ \ \ 23632\ \ \ 2852446\ 2876078}
\DoxyCodeLine{210\ \ \ \ \ 6924\ \ \ \ 2883046\ 2889970}
\DoxyCodeLine{220\ \ \ \ \ 6467\ \ \ \ 2895962\ 2902429}
\DoxyCodeLine{230\ \ \ \ \ 6508\ \ \ \ 2908271\ 2914779}
\DoxyCodeLine{240\ \ \ \ \ 6278\ \ \ \ 2920610\ 2926888}
\DoxyCodeLine{250\ \ \ \ \ 6342\ \ \ \ 2932830\ 2939172}
\DoxyCodeLine{260\ \ \ \ \ 6252\ \ \ \ 2944954\ 2951206}
\DoxyCodeLine{270\ \ \ \ \ 6463\ \ \ \ 2956978\ 2963441}
\DoxyCodeLine{280\ \ \ \ \ 6347\ \ \ \ 2969548\ 2975895}
\DoxyCodeLine{290\ \ \ \ \ 6457\ \ \ \ 2981702\ 2988159}

\end{DoxyCode}


The first column (specimen) is the integer passed to the work1() function when the test is run. The second column (experiment time) is the time, in nanosecond, that work1() takes. The third and fourth columns are self-\/explicit.

You can see that the integer passed work1() starts from 200 and finishes at 290, with a step of 10. These values are computed withe last 3 values passed to \mbox{\hyperlink{group__Eina__Benchmark__Group_ga9732a7da5665e92d0afaf0367d2f6b85}{eina\+\_\+benchmark\+\_\+register()}}. See the document of that function for the detailed behavior.

The gnuplot file will be named bench\+\_\+test\+\_\+run.\+gnuplot. Just run\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{gnuplot\ bench\_test\_run.gnuplot}

\end{DoxyCode}


To create the graphic of the comparison curves. The image file is named output\+\_\+test\+\_\+run.\+png.\hypertarget{tutorial_benchmark_page_tutorial_benchmark_advanced_usage}{}\doxysection{More Advanced Usage}\label{tutorial_benchmark_page_tutorial_benchmark_advanced_usage}
In this section, several test will be created and run. The idea is exactly the same than in the previous section, but with some basic automatic way to run all the benchmarks. The following code benchmarks some Eina converts functions, and some Eina containers types\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{\textcolor{preprocessor}{\#include\ <stdlib.h>}}
\DoxyCodeLine{\textcolor{preprocessor}{\#include\ <stdio.h>}}
\DoxyCodeLine{\textcolor{preprocessor}{\#include\ <time.h>}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{preprocessor}{\#include\ <\mbox{\hyperlink{Eina_8h}{Eina.h}}>}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{static}\ \textcolor{keywordtype}{void}\ bench\_convert(\mbox{\hyperlink{group__Eina__Benchmark__Group_gac5c05887f0ad72bfb28b53e6fde49ce2}{Eina\_Benchmark}}\ *bench);}
\DoxyCodeLine{\textcolor{keyword}{static}\ \textcolor{keywordtype}{void}\ bench\_container(\mbox{\hyperlink{group__Eina__Benchmark__Group_gac5c05887f0ad72bfb28b53e6fde49ce2}{Eina\_Benchmark}}\ *bench);}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{typedef}\ \textcolor{keyword}{struct\ }\_Benchmark\_Case\ Benchmark\_Case;}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{struct\ }\_Benchmark\_Case}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \textcolor{keyword}{const}\ \textcolor{keywordtype}{char}\ *bench\_case;}
\DoxyCodeLine{\ \ \ void\ (*build)(\mbox{\hyperlink{group__Eina__Benchmark__Group_gac5c05887f0ad72bfb28b53e6fde49ce2}{Eina\_Benchmark}}\ *bench);}
\DoxyCodeLine{\};}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{static}\ \textcolor{keyword}{const}\ Benchmark\_Case\ benchmarks[]\ =\ \{}
\DoxyCodeLine{\ \ \{\ \textcolor{stringliteral}{"{}Bench\ 1"{}},\ bench\_convert\ \},}
\DoxyCodeLine{\ \ \{\ \textcolor{stringliteral}{"{}Bench\ 2"{}},\ bench\_container\ \},}
\DoxyCodeLine{\ \ \{\ NULL,\ \ \ \ \ \ NULL\ \}}
\DoxyCodeLine{\};}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{static}}
\DoxyCodeLine{\textcolor{keywordtype}{void}\ convert1(\textcolor{keywordtype}{int}\ request)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \textcolor{keywordtype}{char}\ tmp[128];}
\DoxyCodeLine{\ \ \textcolor{keywordtype}{int}\ i;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \textcolor{keywordflow}{for}\ (i\ =\ 0;\ i\ <\ request;\ ++i)}
\DoxyCodeLine{\ \ \ \ \mbox{\hyperlink{group__Eina__Convert__Group_ga3e89edac8e4955fb645e4690ba05f0a4}{eina\_convert\_itoa}}(rand(),\ tmp);}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{static}}
\DoxyCodeLine{\textcolor{keywordtype}{void}\ convert2(\textcolor{keywordtype}{int}\ request)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \textcolor{keywordtype}{char}\ tmp[128];}
\DoxyCodeLine{\ \ \textcolor{keywordtype}{int}\ i;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \textcolor{keywordflow}{for}\ (i\ =\ 0;\ i\ <\ request;\ ++i)}
\DoxyCodeLine{\ \ \ \ \mbox{\hyperlink{group__Eina__Convert__Group_gab010ab5bc75b33ebeb488ce3b9056a5d}{eina\_convert\_xtoa}}(rand(),\ tmp);}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{static}\ \textcolor{keywordtype}{void}}
\DoxyCodeLine{bench\_convert(\mbox{\hyperlink{group__Eina__Benchmark__Group_gac5c05887f0ad72bfb28b53e6fde49ce2}{Eina\_Benchmark}}\ *bench)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \mbox{\hyperlink{group__Eina__Benchmark__Group_ga9732a7da5665e92d0afaf0367d2f6b85}{eina\_benchmark\_register}}(bench,\ \textcolor{stringliteral}{"{}convert-\/1"{}},\ \mbox{\hyperlink{group__Eina__Benchmark__Group_ga64c3594df1ad868efe1454c34e3e1c37}{EINA\_BENCHMARK}}(convert1),\ 200,\ 400,\ 10);}
\DoxyCodeLine{\ \ \mbox{\hyperlink{group__Eina__Benchmark__Group_ga9732a7da5665e92d0afaf0367d2f6b85}{eina\_benchmark\_register}}(bench,\ \textcolor{stringliteral}{"{}convert-\/2"{}},\ \mbox{\hyperlink{group__Eina__Benchmark__Group_ga64c3594df1ad868efe1454c34e3e1c37}{EINA\_BENCHMARK}}(convert2),\ 200,\ 400,\ 10);}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{static}}
\DoxyCodeLine{\textcolor{keywordtype}{void}\ array(\textcolor{keywordtype}{int}\ request)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \mbox{\hyperlink{struct__Eina__Array}{Eina\_Array}}\ *array;}
\DoxyCodeLine{\ \ \mbox{\hyperlink{group__Eina__Array__Group_gaf9af7cd8afa60d6b1cab805bd5b838aa}{Eina\_Array\_Iterator}}\ it;}
\DoxyCodeLine{\ \ \textcolor{keywordtype}{int}\ *data;}
\DoxyCodeLine{\ \ \textcolor{keywordtype}{int}\ i;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ array\ =\ \mbox{\hyperlink{group__Eina__Array__Group_gaf4b5844225b5359a617744210a5ed6ff}{eina\_array\_new}}(64);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \textcolor{keywordflow}{for}\ (i\ =\ 0;\ i\ <\ request;\ ++i)}
\DoxyCodeLine{\ \ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ data\ =\ (\textcolor{keywordtype}{int}\ *)malloc(\textcolor{keyword}{sizeof}(\textcolor{keywordtype}{int}));}
\DoxyCodeLine{\ \ \ \ \ \ \textcolor{keywordflow}{if}\ (!data)\ \textcolor{keywordflow}{continue};}
\DoxyCodeLine{\ \ \ \ \ \ *data\ =\ rand();}
\DoxyCodeLine{\ \ \ \ \ \ \mbox{\hyperlink{group__Eina__Array__Group_ga97ac1fe353f4cd7a9762af4d4db7b9c4}{eina\_array\_push}}(array,\ data);}
\DoxyCodeLine{\ \ \ \ \}}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \mbox{\hyperlink{group__Eina__Array__Group_ga810ed3618ca03f7d549a1bf2c33b2b6e}{EINA\_ARRAY\_ITER\_NEXT}}(array,\ i,\ data,\ it)}
\DoxyCodeLine{\ \ \ \ free(data);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \mbox{\hyperlink{group__Eina__Array__Group_ga7fa7e2cdcfb739140d5c5a5044086ce1}{eina\_array\_free}}(array);}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{static}}
\DoxyCodeLine{\textcolor{keywordtype}{void}\ list(\textcolor{keywordtype}{int}\ request)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \mbox{\hyperlink{struct__Eina__List}{Eina\_List}}\ *l\ =\ NULL;}
\DoxyCodeLine{\ \ \textcolor{keywordtype}{int}\ *data;}
\DoxyCodeLine{\ \ \textcolor{keywordtype}{int}\ i;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \textcolor{keywordflow}{for}\ (i\ =\ 0;\ i\ <\ request;\ ++i)}
\DoxyCodeLine{\ \ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ data\ =\ (\textcolor{keywordtype}{int}\ *)malloc(\textcolor{keyword}{sizeof}(\textcolor{keywordtype}{int}));}
\DoxyCodeLine{\ \ \ \ \ \ \textcolor{keywordflow}{if}\ (!data)\ \textcolor{keywordflow}{continue};}
\DoxyCodeLine{\ \ \ \ \ \ *data\ =\ rand();}
\DoxyCodeLine{\ \ \ \ \ \ l\ =\ \mbox{\hyperlink{group__Eina__List__Group_ga4a2716e8fba1c3f60e3b112caef26fb9}{eina\_list\_prepend}}(l,\ data);}
\DoxyCodeLine{\ \ \ \ \}}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \textcolor{keywordflow}{while}\ (l)}
\DoxyCodeLine{\ \ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ free(eina\_list\_data\_get(l));}
\DoxyCodeLine{\ \ \ \ \ \ l\ =\ \mbox{\hyperlink{group__Eina__List__Group_gaf86ee0fd9fe0643ece301fc86f04e720}{eina\_list\_remove\_list}}(l,\ l);}
\DoxyCodeLine{\ \ \ \ \}}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keyword}{static}\ \textcolor{keywordtype}{void}}
\DoxyCodeLine{bench\_container(\mbox{\hyperlink{group__Eina__Benchmark__Group_gac5c05887f0ad72bfb28b53e6fde49ce2}{Eina\_Benchmark}}\ *bench)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \mbox{\hyperlink{group__Eina__Benchmark__Group_ga9732a7da5665e92d0afaf0367d2f6b85}{eina\_benchmark\_register}}(bench,\ \textcolor{stringliteral}{"{}array"{}},\ \mbox{\hyperlink{group__Eina__Benchmark__Group_ga64c3594df1ad868efe1454c34e3e1c37}{EINA\_BENCHMARK}}(array),\ 200,\ 300,\ 10);}
\DoxyCodeLine{\ \ \mbox{\hyperlink{group__Eina__Benchmark__Group_ga9732a7da5665e92d0afaf0367d2f6b85}{eina\_benchmark\_register}}(bench,\ \textcolor{stringliteral}{"{}list"{}},\ \mbox{\hyperlink{group__Eina__Benchmark__Group_ga64c3594df1ad868efe1454c34e3e1c37}{EINA\_BENCHMARK}}(list),\ 200,\ 300,\ 10);}
\DoxyCodeLine{\}}
\DoxyCodeLine{}
\DoxyCodeLine{\textcolor{keywordtype}{int}\ main()}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \mbox{\hyperlink{group__Eina__Benchmark__Group_gac5c05887f0ad72bfb28b53e6fde49ce2}{Eina\_Benchmark}}\ *test;}
\DoxyCodeLine{\ \ \mbox{\hyperlink{struct__Eina__Array}{Eina\_Array}}\ \ \ \ \ *ea;}
\DoxyCodeLine{\ \ \textcolor{keywordtype}{unsigned}\ \textcolor{keywordtype}{int}\ \ \ \ i;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \textcolor{keywordflow}{if}\ (!\mbox{\hyperlink{group__Eina__Main__Group_ga81b8137665b8a4d2cb38ca50017d4c74}{eina\_init}}())}
\DoxyCodeLine{\ \ \ \ \textcolor{keywordflow}{return}\ EXIT\_FAILURE;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \textcolor{keywordflow}{for}\ (i\ =\ 0;\ benchmarks[i].bench\_case\ !=\ NULL;\ ++i)}
\DoxyCodeLine{\ \ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ test\ =\ \mbox{\hyperlink{group__Eina__Benchmark__Group_ga0553fada9227bc8b5b9f9c8bfed9def7}{eina\_benchmark\_new}}(benchmarks[i].bench\_case,\ \textcolor{stringliteral}{"{}Benchmark\ example"{}});}
\DoxyCodeLine{\ \ \ \ \ \ \textcolor{keywordflow}{if}\ (!test)}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keywordflow}{continue};}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ \ \ benchmarks[i].build(test);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ \ \ ea\ =\ \mbox{\hyperlink{group__Eina__Benchmark__Group_ga0375144e67c0f4ca6f687236bc845cfe}{eina\_benchmark\_run}}(test);}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ \ \ \mbox{\hyperlink{group__Eina__Benchmark__Group_ga07f1276628b40aac723f21c75b5d5b76}{eina\_benchmark\_free}}(test);}
\DoxyCodeLine{\ \ \ \ \}}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \mbox{\hyperlink{group__Eina__Main__Group_ga5bf35513199d5220341437c05696285d}{eina\_shutdown}}();}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \textcolor{keywordflow}{return}\ EXIT\_SUCCESS;}
\DoxyCodeLine{\}}

\end{DoxyCode}


gnuplot can be used to see how are performed the convert functions together, as well as how are performed the containers. So it is now easy to see that the hexadecimal convert function is faster than the decimal one, and that arrays are faster than lists.

You can improve all that by executing automatically gnuplot in your program, or integrate the Eina benchmark framework in an autotooled project. See that \href{http://trac.enlightenment.org/e/wiki/AutotoolsIntegration\#Benchmark}{\texttt{ page}} for more information. 