I have an implementation of Iterable&lt;T&gt; (a variation of a quadtree structure), which I plan to use in a setting where performance with large data sets is critical, so I have been conducting a few tests, with a couple million random entries, running them repeatedly. I experienced an oddity with the following code segment:   long start = System.currentTimeMillis();  for (int i = 0; i &lt; 100; i++) {      Iterator&lt;A&gt; iter = it.iterator();      while (iter.hasNext()) {          iter.next();      }  }  long end = System.currentTimeMillis();  System.out.println("Total time: " + (end - start));   I always got a time between 4000 and 5000 milliseconds. However, when I changed the while loop to this:  A a = null; while (iter.hasNext()) {     a = iter.next(); }   the time jumps up -- not just slightly, but all the way up to 15 to 16 seconds, with complete consistency. Now this already does not seem dependent on the implementation of next(), but upon further investigation, I found that it even happens with a simple ArrayList, so I'll post compilable code for that instead:  import java.util.ArrayList; import java.util.Iterator; import java.util.List;  public class Test {     static class A {}      public static void main(String[] args) {         List&lt;A&gt; list = new ArrayList&lt;&gt;();         // Add a lot of entries         for (int i = 0; i &lt; 10000000; i++) {             list.add(new A());         }         // Test it         A a = null;         Iterator&lt;A&gt; iter = null;         long start = System.currentTimeMillis();         for (int i = 0; i &lt; 100; i++) {             iter = list.iterator();             while (iter.hasNext()) {                 iter.next();                 // Or:                 // a = iter.next();             }         }         long end = System.currentTimeMillis();         System.out.println("Total time: " + (end - start));     } }   The result: an even more unbelievable 30-fold difference. And it happens deterministically, every time.  What possible reason could there be for this? I don't see how a single assignment to an already allocated variable could be anything other than negligible, especially considering that so much else is happening inside iter.next(). My only guess is that the System.currentTimeMillis() calls are somehow not executed at the proper time, but as for how this is affected by the change, I have no idea.  But even that doesn't quite fit, because the time it takes is noticably much, much longer, especially if I further increase the number of times the for loop runs. The garbage collector also should not have anything more to do, as far as I see, since no wasteful, temporary allocations should be happening. It also apparently is the assignment of the return value that's critical, since just doing something else in addition to iter.next(), like increasing an int variable each time, does not have the same adverse effect on execution time.  EDIT: It has been brought to my attention by multiple people that the particular benchmark in my post suffers from many issues that can compromise its results' trustworthiness. I will leave it here for posterity though, or possibly update it later to make it better. That having been said, the phenomenon's most likely cause has been identified in the accepted answer, and I confirmed that eliminating the typecast solved the issue, so despite the benchmark's shortcomings the observation above appears to not have been just a side-effect of those.
How do you write (and run) a correct micro-benchmark in Java?  I'm looking for some code samples and comments illustrating various things to think about.  Example: Should the benchmark measure time/iteration or iterations/time, and why?  Related:
"Examine whether the given progressions converge (eventually improperly) and determine their limits where applicable.  (a) $$(a_n)_{n\in\mathbb{N}}:=\frac{n^n}{n!}$$  [...]"  I am having problems getting this homework done as I have no clue about convergency at all. Through Mathematica I know that $\lim\limits_{n\to\infty}a_n=+\infty$ however I don't know how to proove it! During my research I found out that for sufficient big $n$ the inequality $x^n\leq n!\leq n^n$ for $x\in\mathbb{R}$ and $n\in\mathbb{N}$ is true. However this doesn't help me at all. Having a look at our lecture notes I developed the following statement: every $x^n$ with $|x| \geq 1$ diverges as $x^n$ is unbounded and therefore has the improper limit $\lim\limits_{n\to\infty}x^n=+\infty$. Furthermore it seems obvious to me that I need to express the terms in a simpler way, like fractions converging to 0 etc, but I don't know how. Neither some work with the $\varepsilon$-definition of limits in combination with the triangle inequality helped me out. Any suggestions or hints?  Information: This is "Analysis for computer scientist" and therefore we haven't "officially" learned any fancy tricks like L'Hôpital's rule etc. just basic properties of $\mathbb{R}$ and some inequalities as this is an introductory lecture. Complete classification of the groups for which converse of Lagrange's Theorem holds Trichromelibrary was suddenly downloading from Google Play Store I've got a utility that outputs a list of files required by a game.  How can I run that utility within a C program and grab its output so I can act on it within the same program? UPDATE: Good call on the lack of information.  The utility spits out a series of strings, and this is supposed to be portable across Mac/Windows/Linux.  Please note, I'm looking for a programmatic way to execute the utility and retain its output (which goes to stdout). Why do I have blurred vision and how do I fix it? How to know when to change a chain? A bike shop employee used a tool he said would check the wear on the chain, and said that it should be changed.   He said it took a measurement that meant the chain had expanded, but it is still hard for me understand why I should change the chain.   He said that the reason was that the expanded chain links will cause the chain rings to wear down.   That is one argument and I have no experience with it, to know if it is true.   But are there more reasons? How do I know when the chain should be changed?   My first thought is to use it until it breaks, but it probably is not the wisest option. 2 separate lights on two separate switches on one line - issue Using k-fold cross-validation for time-series model selection Question: I want to be sure of something, is the use of k-fold cross-validation with time series is straightforward, or does one need to pay special attention before using it?   Background: I'm modeling a time series of 6 year (with semi-markov chain), with a data sample every 5 min. To compare several models, I'm using a 6-fold cross-validation by separating the data in 6 year, so my training sets (to calculate the parameters) have a length of 5 years, and the test sets have a length of 1 year. I'm not taking into account the time order, so my different sets are :   fold 1 : training [1 2 3 4 5], test [6] fold 2 : training [1 2 3 4 6], test [5] fold 3 : training [1 2 3 5 6], test [4] fold 4 : training [1 2 4 5 6], test [3] fold 5 : training [1 3 4 5 6], test [2] fold 6 : training [2 3 4 5 6], test [1].   I'm making the hypothesis that each year are independent from each other. How can I verify that? Is there any reference showing the applicability of k-fold cross-validation with time series. Can grep output only specified groupings that match? Say I have a file:  # file: 'test.txt' foobar bash 1 bash foobar happy foobar   I only want to know what words appear after "foobar", so I can use this regex:  "foobar \(\w\+\)"   The parenthesis indicate that I have a special interest in the word right after foobar.  But when I do a grep "foobar \(\w\+\)" test.txt, I get the entire lines that match the entire regex, rather than just "the word after foobar":  foobar bash 1 foobar happy   I would much prefer that the output of that command looked like this:  bash happy   Is there a way to tell grep to only output the items that match the grouping (or a specific grouping) in a regular expression? Substitute for onions and garlic I love the taste of onions and garlic and it seems lots of other people do too.  But they upset my stomach so much that I can't really cook with them.  What can I use in their place to give my food a similar flavour? I have quite a bad habit of copy-and-pasting my packages from one LaTeX document to another. Is there any way we can detect unused packages (i.e. \usepackage commands) so that they can be removed from the LaTeX document? I'm trying to build a datapack that lets you craft xp bottles from glass bottles at the expense of 7 xp per bottle (because they drop 3 to 11 xp so the average is 7). I have almost everything working except for a check for if they have too little xp. If they have 7 or less xp, I would like to remove one bottle of enchanting from their inventory and give them one glass bottle, because the function for removing xp is called after they've crafted one and a scoreboard has counted it. My remove function currently looks like this:  #create dummy scoreboard to multiply bottles crafted by seven to get xp to be removed scoreboard objectives add 7xp dummy scoreboard players set @s 7xp -7 scoreboard players operation @s xpCrafted *= @s 7xp  #how do I get the value in xpCrafted into the command to remove xp? xp add @s -7 points  #cleanup scoreboard players reset xpCrafted scoreboard players reset 7xp scoreboard players set @e xpCrafted 0 scoreboard objectives remove 7xp   And my tick function like this:  execute as @e[scores={xpCrafted=1..}] at @s run function craft_xp:removexp   As the inline comment says, how do I get that value in xpCrafted into my xp removing command, and also how should I check to make sure the player has enough xp for the operation in the tick command (or elsewhere)? Presumably I would run a different command to remove the extra bottle if they didn't have enough xp, but I don't know how because there doesn't seem to be any type of if/else structure in commands. Finally, should a question like this be in two different questions or is one ok on this forum? Regular expression to match a line that doesn't contain a word Looking for a Simple Argument for "Integral Curve Starting at A Singular Point is Constant" Let $U$ be an open subset of $\mathbf R^n$ and $V:U\to\mathbf R^n$ be a differentiable vector field on $U$. Let $\mathbf p\in U$ be a singular point of $V$, that is, $V(\mathbf p)=\mathbf 0$.     Then the only integral curve which starts at $\mathbf p$ is the constant curve.   I know that one could simply use the theorem of "uniqueness of integral curves" and in fact adapt the same proof for this particular case.  But is anybody aware of a simple argument for this? How do you judge the quality of a journal? Temporarily change language for terminal messages/warnings/errors Messages in my terminal are displayed using Russian language by default, which is my native one.  For just a moment I want them to be in English (eg. to paste in forums), then switch back to the default language.  How can I do the switch and switch back using bash? Find the limit $$\lim_{x\to 0^{+}}\dfrac{\sin{(\tan{x})}-\tan{(\sin{x})}}{x^7}$$  My attempt: Since $$\sin{x}=x-\dfrac{1}{3!}x^3+\dfrac{1}{5!}x^5-\dfrac{1}{7!}x^7+o(x^7)$$ $$\tan{x}=x+\dfrac{1}{3}x^3+\dfrac{2}{15}x^5+\dfrac{1}{63}x^7+o(x^3)$$ So $$\sin{(\tan{x})}=\tan{x}-\dfrac{1}{3!}(\tan{x})^3+\dfrac{1}{5!}(\tan{x})^5-\dfrac{1}{7!}(\tan{x})^7+o(x^7)$$ Though this method might solve, I think this problem has nicer methods. Thanks. I use LINQ to Objects instructions on an ordered array.  Which operations shouldn't I do to be sure the order of the array is not changed? Prime factorization knowing n and Euler's function My task is to factorize $n$ knowing it has two factors and $\varphi(n)$. How can I do it?  Thanks for any advice. I currently have a setup where I send a char using a Tx of 434MHz and an Uno to a Mega with a Rx. The Mega counts how many times it receives the char and then if it falls below a certain number it triggers an alarm. Is this a viable way to measure the distance between two microcontrollers while indoors or is there a better way.  Transmitter (Mega)  #include &lt;SoftwareSerial.h&gt;  int rxPin=2; //Goes to the Receiver Pin int txPin=5; //Make sure it is set to pin 5 going to input of receiver  SoftwareSerial txSerial = SoftwareSerial(rxPin, txPin); SoftwareSerial rxSerial = SoftwareSerial(txPin, rxPin); char sendChar ='H';  void setup() {   pinMode(rxPin, INPUT);   pinMode(txPin,OUTPUT);   txSerial.begin(2400);   rxSerial.begin(2400); }  void loop() {   txSerial.println(sendChar);   Serial.print(sendChar); }   Receiver  #include &lt;SoftwareSerial.h&gt;  //Make sure it is set to pin 5 going to the data input of the transmitter int rxPin=5;  int txPin=3; //Don't need to make connections int LED=13; int BUZZ=9; int t=0;  char incomingChar = 0; int counter = 0;  SoftwareSerial rxSerial = SoftwareSerial(rxPin, txPin);  void setup() {   pinMode(rxPin, INPUT); //initilize rxpin as input   pinMode(BUZZ, OUTPUT); //initilize buzz for output   pinMode(LED, OUTPUT); //initilize led for output   rxSerial.begin(2400); //set baud rate for transmission   Serial.begin(2400); //see above }  void loop() {   for(int i=0; i&lt;200; i++) {     incomingChar = rxSerial.read(); //read incoming msg from tx      if (incomingChar =='H') {       counter++; //if we get bit "h" count it       }     delay(5); //delay of 10 secs   }   Serial.println(incomingChar);   Serial.println(counter); //prints the the bits we recieved    if(counter&lt;55) {     //if we receive less than 100 bits than print out of range triggers alarm     Serial.println("out of range");      tone(BUZZ,5000,500);digitalWrite(LED,HIGH);   }   else {     noTone(BUZZ);digitalWrite(LED, LOW);     //if we get more than 100 bits than we are within range turn off alarm     Serial.println("in range");    }    counter = 0;   incomingChar=0; }