In leetcode, for the add two numbers problem, I implemented the same algorithm both in Java and Scala. But Java version outperforms scala a lot. The runtime of java version code is 52ms while the runtime of scala one is 552ms. Why is there huge performance difference?  Codes are attached:  Scala version:  object Solution { def addTwoNumbers(l1: ListNode, l2: ListNode): ListNode = { var a = l1 var b = l2 var sum = 0 var digit = 0 var tenDigit = 0 val l3 = new ListNode var c = l3 while(a!=null || b!=null){    sum = (if(a==null)0 else a._x) + (if(b==null)0 else b._x) + tenDigit   digit = sum % 10   tenDigit = sum / 10    c.next = new ListNode(digit)   c = c.next   if(a!=null)a = a.next   if(b!=null)b = b.next  }  if (tenDigit &gt; 0) {     c.next = new ListNode(tenDigit); }  l3.next  } }   Java version:  class Solution { public ListNode addTwoNumbers(ListNode l1, ListNode l2) { ListNode a = l1; ListNode b = l2; int sum = 0; int digit = 0; int tenDigit = 0; ListNode l3 = new ListNode(0);     ListNode zeroNode = new ListNode(0); ListNode c = l3; while(a!=null || b!=null){  sum = (a==null ? 0 : a.val) + (b==null ? 0 : b.val) + tenDigit; digit = sum % 10; tenDigit = sum / 10;  c.next = new ListNode(digit); c = c.next; if(a!=null)a = a.next; if(b!=null)b = b.next;  } if(tenDigit!=0){    c.next = new ListNode(tenDigit); } return l3.next; } }
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:
What happens to half of the energy in a circuit with a capacitor? For a simple circuit with a battery supplying a voltage $V$ to a capacitor, let us assume that the charge on the capacitor is $Q$. Now, the work done by the battery or the energy supplied is given by the relation:  $$W=QV$$  But the energy stored in the capacitor is given by:  $$U = \tfrac12 QV$$  The value of $Q$ as well as that of $V$ should be the same in both the equations.  Now my question is, where is the other half of the energy that the battery supplied? Why are some applications so hard to find in the Ubuntu Software Centre? Some applications don't pop up in the search even when you type in their name. Take for instance Sushi. When you type in "sushi" into the search box, it fails to throw up the application. Only when you type in "gnome-sushi" does it appear. This is despite the fact that the application has "sushi" by itself in the application description. There are other applications which don't spring to mind right now, but I've discovered before. Evaluating the product $\prod\limits_{k=1}^{n}\cos\left(\frac{k\pi}{n}\right)$ Recently, I ran across a product that seems interesting.  Does anyone know how to get to the closed form:  $$\prod_{k=1}^{n}\cos\left(\frac{k\pi}{n}\right)=-\frac{\sin(\frac{n\pi}{2})}{2^{n-1}}$$  I tried using the identity $\cos(x)=\frac{\sin(2x)}{2\sin(x)}$ in order to make it "telescope" in some fashion, but to no avail. But, then again, I may very well have overlooked something.  This gives the correct solution if $n$ is odd, but of course evaluates to $0$ if $n$ is even.   So, I tried taking that into account, but must have approached it wrong.   How can this be shown? Thanks everyone. In the example  $\cup^{i=1}_{\infty}F_i$   How do you get the i=1 directly below the union symbol and the infinity directly above? Advanced search in Google Contacts Is there any way to do perform advanced searches in Google Contacts?  For example, wild card operators, AND operator, OR operator etc?  I want to search anywhere in a name, not just the beginning of a name. (i.e if I write 'ac' it should display Jack as well) How can I execute a file without giving myself execute permissions (with chmod u+x) for it?   If I try and tick the 'Allow executing file as program' checkmark, the checkmark is immediately removed. In what order should you do linear regression diagnostics? In linear regression analysis, we analyze outliers, investigate multicollinearity, test heteroscedasticty.   The question is:  Is there any order to apply these? I mean, do we have to analyze outliers very firstly, and then examine multicollinearity? Or reverse?    Is there any rule of thumb about this? Adjective, if adjective: eg "pointed, if fatherly" They were pointed, if fatherly, remarks that echoed the themes he has stressed in his papacy but ones that resonated all the more in a newly renovated cathedral surrounded by the luxurious shops of Fifth Avenue. —   I'm not sure the meaning of the phrase &quot;if fatherly&quot; in the above sentence. I'm not familiar with the syntax using the structure 'adjective, if adjective'. How to make pages metabox display all hierarchy in menu admin? Running WP 3.9.1  I've got lots of pages and sub pages.  When I get to create menu lots of sub pages doesn't display on the right level, but some do?    The pages metabox should display something more like this    EDIT  Number in screenshot are a bit screwed sorry about that.   The problem is after the first few all the pages are displayed on the same level but they shouldn't.  It should look like this       Page 1                      SubPage A of Page 1             SubPage B of Page 1                                      SubPage 1 of SubPage B                     SubPage 2 of SubPage B                     SubPage 3 of SubPage B                                                 Page 2                      SubPage A of Page 2                                      SubPage 1 of SubPage A                     SubPage 2 of SubPage A                                           SubPage B of Page 2                                      SubPage 1 of SubPage B                     SubPage 2 of SubPage B                                               ...    Any idea what cause this or how to fix it, so i can manage my menu? When should I use # and = in ASP.NET controls? I am constructing a bibliography of as much of the prior research in a field I am working in. I would like to use BibTeX to print the citation in the pdf, and then I would like to add the details of the paper after the citation. Only then would I like to print the next citation. For example:  [1] Jahan M. Dawlaty and Yunquing Chen. Measurement of the optical absorption of graphene in the terahertz. Applied Physics Letters, 2008. Research Group: Cornell Pump Wavelength: 500 nm. Probe Wavelenth: 7 microns. Relaxation Time: 50 fs. [2] Paul George and Dmitry Vekslar, Measurement of Ultrafast Carrier Dynamics in Graphene. Applied Physics Letters, 2009.  . . . and so on. Does anyone know how this could be done? How can a file be copied? I have a friend who is playing with me on a server we've created. Unfortunately, he is on a  Mac whereas I am on a Windows machine. I told him that I should be able to compile and send a .minecraft folder to him so that we can both play. Unfortunately, he can't find his .minecraft folder where it is supposed to be. Is it hidden and how can I unhide it? A set $A \subset l_1$ is compact if and only if closed, bounded, and one other condition A set $A \subset \ell_1$ is compact if and only if $A$ is closed and bounded and given any $\epsilon &gt;0$, there exists $n_0$ such that $\sum_{k=n}^{\infty} |x_k| &lt; \epsilon$ for all $n&gt; n_0$ and $x \in A$.   It's easy to prove the part assuming that $A \subset l_1$ is compact, but I am finding difficulty to prove the other part. Immutability, Association and typing Finding a tricky limit I have a homework in real analysis and I'm very confused about it. I would be very thankful, if you could give me any ideas or solutions how to get this task done. The task is as follows:     Find the limit:   $$\lim_{x\to 0} \frac{1}{x} \int_{0}^{x} (1+ \sin(t))^{1/t} dt.$$   A hint that has been given is to use L'Hospital's Rule. Moreover, it must be substantiated why we can use L'Hospital' Rule in this situation. It is also important to mention that $(1 + \sin(t))$ has a removable point of discontinuity at $0$ and therefore the integral can be interpreted as a Riemann integral. If GCD $(a_1,\ldots, a_n)=1$ then there's a matrix in $SL_n(\mathbb{Z})$ with first row $(a_1,\ldots, a_n)$ Let $n \geq 2$. Let $a_1, a_2, \ldots, a_n$ be $n$ integers such that $\gcd\left(a_1, a_2, \ldots, a_n\right) = 1$. Prove that there exists a matrix in $\operatorname{SL}_n\left(\mathbb{Z}\right)$ whose first row is $\left(a_1, a_2, \ldots, a_n\right)$.   Since the gcd of the integers $a_1,\ldots, a_n$ is $1$, there exists weights $x_i \in \mathbb{Z}$ such that $a_1x_1+\cdots+ a_nx_n=1$. My two ideas are (a) to brute force construct an $n\times n$ matrix with first row $a_1,\ldots ,a_n$ and to construct the remaining rows such that the determinant is $\sum a_ix_i=1$ or (b) to use induction.  (a) (Constructive) This is tedious since once I find a way to construct the remaining $n-1$ rows to ensure that $a_1x_1$ appears in the determinant, I am not sure how to modify these $n-1$ rows to ensure  that only the terms $a_ix_i$ appear in the cofactor expansion. If such a matrix exists, I'd like to see it.  (ii) (Non-constructive) If I proceed by induction then the base case $n=2$ is settled since I can choose the 2nd row to be $-x_2, x_1$ so that the determinant is $a_1x_1-a_2(-x_2)=1$. However, I'm not sure how to use the inductive hypothesis to show that if I can construct such an $n\times n$-matrix then I can construct an $\left(n+1\right) \times \left(n+1\right)$-matrix with the desired property. In particular, if the gcd $(a_1,\ldots ,a_{n+1})$ is $1$, it is not necesarry that the gcd of any $n$ of these terms is $1$, so induction may not even apply here.  How can I construct such a matrix or prove that one exists (without necessarily constructing it)? Downloading PlayOnLinux I'm downloading PlayOnLinux to run Paint Tool SAI, but I cannot open PlayOnLinux. When I click on it to open it, a dialog box comes up that says: "PlayOnLinux cannot find curl (from cURL) You need to install it to continue". How do I install this?   I'm referring to this article: I have some questions regarding suspended users:   Why do suspended users have a reputation of one? According to the default reputation of a new user, he/she will have reputation of one and he/she has the privilege of posting questions. When comparing this, this is getting violated when compared to suspended users' reputation. Even though they have one reputation point, they cannot post questions. As suspended users do not have privilege of posting questions, there is no use of making "Ask Question" visible. This is an unnecessary server hit. Am I right? As suspended users do not have the privilege of answering questions, there is no use of making "Answer Question" visible. Exact sequence of sheaves with non exact sequence of global sections Let $X$ be some topological space. By $\mathcal{F}_i$ we denote some sheaves of abelian groups on $X$. The sequence of sheaves and morphisms  $$\mathcal{F}_1\longrightarrow \mathcal{F}_2\longrightarrow \mathcal{F}_3\longrightarrow... $$ is said to be exact if for each $x\in X$ the corresponding sequence of stalks $$(\mathcal{F}_1)_x\longrightarrow (\mathcal{F}_2)_x\longrightarrow (\mathcal{F}_3)_x\longrightarrow... $$ is exact. However if the sequence of sheaves is exact than the sequence of global sections is not necessarily exact! (The most famous example is the sequence of sheaves $$0\longrightarrow\mathbb{Z}\hookrightarrow \mathcal{O}\stackrel{\exp}{\longrightarrow} \mathcal{O}^*\longrightarrow0$$ considered as sheaves on $\mathbb{C}-\{0\}$, where $\mathcal{O}$ is a sheaf of holomorphic functions, $\mathcal{O}^*$ is a sheaf of holomorphic functions with no zeros).  So, could you give me some easy examples of such phenomenon ?