Tuesday, March 26, 2013

The protocol of IProgressMonitor


Very nice write-up on this:
http://www.eclipse.org/articles/Article-Progress-Monitors/article.html
http://www.eclipse.org/articles/Article-Progress-Monitors/article.html#Ensure_to_always_complete_your_monitor


One contract pattern described above is that if beginTask() is ever called, done() MUST be called. This is achieved by always following this code pattern (all code is simplified):
monitor = … // somehow get a new progress monitor which is in a pristine state
// figure some things out such as number of items to process etc…
try
 {
 monitor.beginTask(…)
 // do stuff and call worked() for each item worked on, and check for cancellation
 }
finally
 {
 monitor.done()
 }

Tuesday, March 19, 2013

Javascript functions and difference



The two ways to declare a java script function:
var functionOne = function() {
    // Some code
};
function functionTwo() {
    // Some code
}


The difference is that functionTwo is defined at parse-time for a script block, whereas functionOneis defined at run-time. For example:
<script>
  // Error
  functionOne();

  var functionOne = function() {
  }
</script>

<script>
  // No error
  functionTwo();

  function functionTwo() {
  }
</script>




Tuesday, February 26, 2013

StringBuilder with modern compilers



Modern Java compiler convert your + operations by StrinBuilder's append. I mean to say if you do str = str1+sr2+str3 then compiler will generate following code
StringBuilder sb = new StringBuilder();
str = sb.append(str1).append(str2).append(str3).toString();
So now its more of matter of choice than performance benefit to use + or StringBuilder :)

JVM: Finding the process id


package com.kk.memory.tool;

import sun.management.VMManagement;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class FindJavaProcessId {

public static void main(String[] args) {

try {
RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
Field jvmField = runtimeMXBean.getClass().getDeclaredField("jvm");
jvmField.setAccessible(true);
VMManagement vmManagement = (VMManagement) jvmField.get(runtimeMXBean);
Method getProcessIdMethod = vmManagement.getClass().getDeclaredMethod("getProcessId");
getProcessIdMethod.setAccessible(true);
Integer processId = (Integer) getProcessIdMethod.invoke(vmManagement);
System.out.println("################    ProcessId = " + processId);
} catch (Exception e) {
e.printStackTrace();
}
}

}

String vs StringBuilder performance trade-offs


public class StringTest {

public static void main(String[] args) {

int[] runfor = { 10, 100, 1000, 10000 };

for (int i = 0; i < runfor.length; i++) {
long start = System.currentTimeMillis();
String s = "";
for (int j = 0; j < runfor[i]; j++) {
s += "Hello" + "--" + "World!!!";
}
long end = System.currentTimeMillis();
System.out.println("String concatenation x " + runfor[i] + " times: " + (end - start) + " miliseconds");

start = System.currentTimeMillis();
StringBuilder sb = new StringBuilder();
for (int j = 0; j < runfor[i]; j++) {
sb.append("Hello");
sb.append("--");
sb.append("World!!!");
}
end = System.currentTimeMillis();
System.out.println("StringBuilder concatenation x " + runfor[i] + " times: " + (end - start) + " miliseconds");
}
}
}


Results:

String concatenation x 10 times: 0 miliseconds
StringBuilder concatenation x 10 times: 0 miliseconds
String concatenation x 100 times: 1 miliseconds
StringBuilder concatenation x 100 times: 0 miliseconds
String concatenation x 1000 times: 15 miliseconds
StringBuilder concatenation x 1000 times: 1 miliseconds
String concatenation x 10000 times: 1143 miliseconds
StringBuilder concatenation x 10000 times: 3 miliseconds

Monday, January 28, 2013

Pushing a specific commit to repository


git status says:

$ git status
# On branch master
# Your branch is ahead of 'origin/master' by 2 commits.

You want to git push only one of those commits to the public repo. Here’s how:

First use git log to get the commit hash of the commit you want to push. Then:

$ git push origin <thelonghash>:master

example:
$ git push origin 7605596d45d2e3812da1e22db447fc6f1fe6f876:Dev-5.0

Friday, January 25, 2013

Viewing Unpushed Git Commits


It was useful for me to avoid unnecessary commits with a push.


git log origin/master..HEAD
You can also view the diff using the same syntax
git diff origin/master..HEAD


Source:  http://stackoverflow.com/questions/2016901/viewing-unpushed-git-commits