<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=us-ascii">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Aptos;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0cm;
        font-size:11.0pt;
        font-family:"Aptos",sans-serif;
        mso-ligatures:standardcontextual;
        mso-fareast-language:EN-US;}
span.EmailStyle18
        {mso-style-type:personal-compose;
        font-family:"Aptos",sans-serif;
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-size:11.0pt;
        mso-ligatures:none;
        mso-fareast-language:EN-US;}
@page WordSection1
        {size:612.0pt 792.0pt;
        margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]-->
</head>
<body lang="EN-CA" link="#467886" vlink="#96607D" style="word-wrap:break-word">
<div class="WordSection1">
<p class="MsoNormal"><span lang="EN-US">Hi everyone,<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">Thank you for allowing me to join this mailing list.<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">My name is Ken Fogel and I was a Computer Science instructor at Dawson College in Montreal for 31 years, having retired 3 years ago. I was chairperson for 25 of those years and led the change from COBOL to Java in 2000.
 I am a Java Champion and organize, with fellow JCs, the JChampions Conference. I also sit on the JCP Executive Committee. Like all retired academics I wrote a book entitle Transitioning to Java from Packt for devs who need to move to Java from any other language.
 Finally, I still get to present at conferences, and it is work I am doing with the JMH for JavaLand that has led me to this mailing list.<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">Some years ago, I wrote a program that demonstrated the relative differences between the various collection in Java. The program worked as required, made for a good lecture, and proved why no one should use a LinkedList
 (this remark is meant to be funny and true). A little over a year ago I discovered the jmh and created a version of my program using it. Unfortunately, I could not run the benchmarks as I did in my version as the results were presented in a Swing table but
 that is not the issue I am having. Here is the measurement code for adding an element at the end of an ArrayList from my pre-jmh code:<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">for (int y = 0; y < REPETITIONS; ++y) {<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">         arrayDeque = new ArrayDeque<>();<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">         for (int x = 0; x < SIZE; ++x) {<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">                arrayDeque.add(dataArray[x]);<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">         }<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">         startTime = System.nanoTime();<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">         arrayDeque.addFirst("Dawson College");<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">         endTime = System.nanoTime() - startTime;<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">         runningTotal += endTime;<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">  }<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">The dataArray is a simple array of 10000 strings. I use the same set of words for every test. This worked well enough. The actual time to insert was calculated after a new ArrayList was created and populated for every
 repetition. This was necessary because had I created a new copy with :<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">arrayDeque = new ArrayDeque<>(Arrays.asList(dataArray));<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">I end up with an ArrayDeques that is the same size as the Array I am initializing it with. The problem is that this ArrayDeque has no slack space so it must immediately grow, As I am interested in the performance when
 there is slack space, I create the ArrayDeque in a loop. This results in the ArrayDeque having slack space.<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">When I moved to the jmh I could not find a way to create and initialize the ArrayList as @Benchmark timed the entire method’s code.  I thought I figured it out by using an inner static class annotated with @State. It
 did not work. I discovered that the ArrayList was being initialized for each iteration but, and this was a very big but, I discovered after reading numerous StackOverflows and then looking at the jmh source code that iterations that were not set to  “Mode.SingleShotTime”
 executed the @Benchmark method a random number of times per iteration. This would be fine except that the @State class/method was only invoked once per iteration and not every time the method was executed as I required.
<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">If you have read this far you can probably guess my question. Is it possible to execute code that is part of a @Benchmark method but that is not included in the timing. Is there or could there be an annotation for this
 rather than use SingleShotTime and then have a large value for measurementIterations . Something like:<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">@Benchmark<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">public void do10InsertLastElementArrayList(Blackhole blackhole) {<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><b><span lang="EN-US">       @IgnoreOn<o:p></o:p></span></b></p>
<p class="MsoNormal"><b><span lang="EN-US">         Deque <String> arrayDeque = new ArrayDeque<>();<o:p></o:p></span></b></p>
<p class="MsoNormal"><b><span lang="EN-US">         for (int x = 0; x < SIZE; ++x) {<o:p></o:p></span></b></p>
<p class="MsoNormal"><b><span lang="EN-US">                arrayDeque.add(dataArray[x]);<o:p></o:p></span></b></p>
<p class="MsoNormal"><b><span lang="EN-US">         }<o:p></o:p></span></b></p>
<p class="MsoNormal"><b><span lang="EN-US">        @IgnoreOff<o:p></o:p></span></b></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">      arrayDeque.add("Dawson College");<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">      blackhole.consume(arrayDeque); // Not certain I need this<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">        <o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">    }<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">If the operation does not mutate anything then there is no issue. It is when I mutate something that I need to restore the state of variables to before the mutation every time and not have this included in the timing
 measurement.<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">Thank you for your patience in reading this and for any assistance you can provide.
<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
<p class="MsoNormal"><span lang="EN-US">Ken<o:p></o:p></span></p>
<p class="MsoNormal"><span lang="EN-US"><o:p> </o:p></span></p>
</div>
</body>
</html>