Sunday, March 31, 2013
Improving performance of Java String methods
Some of Java String methods use regular expressions e.g. matches(...), replaceFirst(...), replaceAll(...) and split(...). These methods actually use Pattern matching library methods internally however these patterns are parsed every time these methods are invoked. The performance impact is significant if these methods are called too frequently or in high traffic zone.
Java library provides Pattern package which could be used to precompile the regular expressions.
Here are equivalent code which uses the precompiled pattern.
1) String.split(regex)
[java]
private static final String regex = "\\.";
String[] keys = str.split(regex);
[/java]
[java]
import java.util.regex.Pattern;
private static final Pattern myPattern = Pattern.compile(regex);
String[] keys = myPattern.split(str);
[/java]
2) String.replace*(regex...)
2.1) replaceFirst() - http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#replaceFirst(java.lang.String, java.lang.String)
[java]
str.replaceFirst(regex, repl) yields exactly the same result as the expression
"Pattern.compile(regex).matcher(str).replaceFirst(repl)""
[/java]
[java]
private static final String regex = "\\.";
private static final Pattern myPattern = Pattern.compile(regex);
myPattern.matcher(str).replaceFirst(repl);
[/java]
2.2) replaceAll() - http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#replaceAll(java.lang.String, java.lang.String)
[java]
str.replaceAll(regex, repl) yields exactly the same result as the expression
"Pattern.compile(regex).matcher(str).replaceAll(repl)""
[/java]
[java]
private static final String regex = "\\.";
private static final Pattern myPattern = Pattern.compile(regex);
myPattern.matcher(str).replaceAll(repl);
[/java]
3) matches(regex) - http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#matches(java.lang.String)
[java]
str.matches(regex) yields exactly the same result as the expression
"Pattern.matches(regex, str)"
[/java]
[java]
private static final String regex = "\\.";
private static final Pattern myPattern = Pattern.compile(regex);
myPattern.matches(regex, str);
[/java]
Sunday, January 13, 2013
getting started with GIT - an introduction
Git is an open source distributed version control system.
The screen shots were taken from youtube presentation by Scott Chacon.
Traditional version control systems used to have a single storage where all the developers used to connect and operate. This lead to a single point of failure.
In git, there is no single server which maintains the history. All parties/developers's machine become node in the distributed system and they can work in isolated fashion as well. Basically the system behaves as loosely coupled.
Below is the comparison between traditional VCS and Git. Git stores the file contents along with the checksum which relieves it from managing the change log and plenty of indirected connections between file versions.
Git stores all the checksum as directed graph which helps it easily identify what and how to merge code.
Below are the basic commands for doing any file modifications:
when we do 'git checkout' it creates index database and then creates a local directory where stores all the contents. Running 'git add' puts the file contents along with the checksum in the Index database. The final 'git commit' does a commit from Index database to local repository. Note that 'commit' doesn't look at the local files, it only looks at Index database for final commit to repository. What it means is after doing 'add', we can delete the local file however 'commit' will still continue without loosing the change.
Merging: git does merging by looking at the changes between current branch 'master' and the branch to be merged 'branchA'. It basically walks through the directed graph and tries to find the common ancestor. If found then it does a fast-forward merge which means it moves the head pointer to 'branchA'.
Simple merge (fast forward). merge of branch iss53:
Simple delete: if Head can reach the branch directly then it deletes else skips. so it has a safety mechanism.
git branch -d iss53
i18n can't be reached from head so using below command forcefully deletes it. Hard delete.
git branch -D i18n
steps of code push.
scott pushes the change
git does a fast-forward merge in origin master repo because it sees a common ancestor between origin master and scott's master.
Now Nick tries to push but that will fail as origin master in Nic's local repo and in origin remote repo are pointing to different commit versions.
so Nick does a 'git fetch' first.
then he does merge. because (pull = fetch + merge).
now things look good and origin master points to right commit version. and hence the collaborated delivery completed gracefully,
How to create Aliases?
Git subsets
answer is: git log i18n ^master
What is the change in iss53 which is not in i18n?
answer is the highlighted commit histories:
...Quizes...
Git Incoming? its the change which is in origin/master and not in local master. solution: fetch will pull in the change.
here is how to check what changes we are missing in local:
git log origin/master ^master
Git outgoing? its the change which is in local master and still not got pushed to origin/master. solution: git push will deliver the change
here is how to check what changes we still did not merge to origin/master from local:
git log master ^origin/master
Tuesday, December 4, 2012
Automatically take screenshots every few seconds from a video
The MPlayer tool could be used to do it on a command line.
download mplayer from http://www.mplayerhq.hu/design7/dload.html
The below command will capture the frame at 10 seconds interval till the end of the video (video length - 1 second):
mplayer -vo jpeg -sstep 10 -endpos 499 myvideo.avi --- suppose video is 500 seconds long
People could use this tool to generate pictures out of video and upload to get feedback from their friends/users.
Monday, January 16, 2012
Login to your Google Account on Without Typing Anything
Logging in to your accounts using public computers may be at risk due to virus, key loggers or any other programs which may collect your account credentials and later compromise your account.
Today Google came up with the novel idea “allowing users to login using their trusted phones”.
Here is the solution:
Step 1: open https://accounts.google.com/sesame on the computer which you feel might be at risk.
opening the above link will show a QR code.

Step 2: make sure you have smart phone with QR code reader application (e.g. Google Goggle, Bakodo, Red Laser etc). Scan the bar code shown on your computer screen (from Step 1).
Step 3: you will be shown a URL to open and sign in with your google account (Gmail or iGoogle).
Step 4: within a few moments, Google will sign-in the page and your page on screen will redirected to either Gmail or iGoogle as per your selection in Step 3.
Step 5: now continue using your google account on the computer.
Step 6: After you are done your work on the computer, don’t forget to sign off manually.
All is fine so far and loved the creative way to reduce the risk of account take over. This is fantastic and a great way to avoid risk.
However I did not like “Step 6”, why? its because sign-up and sign-in are done differently and from different devices. Since Google has knowledge about both of these devices, so why can’t allow sign off using my device? what if I forgot to sign-off from computer, any way to do the same from my phone device which was used to sign-in? how about when user marks in his device for sign-off and the next action on google account (related to the session with the same QR code) on the computer logs off automatically?
This may be another checkpoint where there needs some improvements.
Google has solved its problem here however the same problem exists for other websites and for whole internet space. The internet needs it badly, to make surfing a better experience.
Hope Google still working on this to make it a general solution which whole industry could benefit.
Well done and Good Luck Google!!!
-a programmer’s opinion.

























