Category Archives: kool stuff

Branch predictor

Whenever in our code a conditional statement is executed, Branch Precitor tries to predict which way the flow will go and executes that even before conditional statement actually returns the output. This is done to stop wastage of time in waiting for conditional statement execution.


From wikipedia
– In computer architecture, a branch predictor is a digital circuit that tries to guess which way a branch (e.g. an if-then-else structure) will go before this is known for sure. The purpose of the branch predictor is to improve the flow in the instruction pipeline. Branch predictors play a critical role in achieving high effective performance in many modern pipelined microprocessor architectures such as x86.

Here is a good example to see what Branch predictor can do

package Permissions.Permissions;

import java.util.Arrays;
import java.util.Random;

public class test {

public static void main(String[] args)
{
// Generate data
int arraySize = 32768;
int data[] = new int[arraySize];

Random rnd = new Random(0);
for (int c = 0; c < arraySize; ++c) data[c] = rnd.nextInt() % 256; // !!! With this, the next loop runs faster // Arrays.sort(data); // Test long start = System.nanoTime(); long sum = 0; for (int i = 0; i < 100000; ++i) { // Primary loop for (int c = 0; c < arraySize; ++c) { if (data[c] >= 128)
sum += data[c];
}
}

System.out.println((System.nanoTime() - start) / 1000000000.0);
System.out.println("sum = " + sum);
}

}

Try uncommenting the sort array part and see the difference.

Source- stackoverflow

Split a String in controlled manner

You must have used String’s split method a lot of time to split based on a regex or a simple character or sub-string. But did you know that you can apply the split function in controlled manner. i.e. you can specify how many time split should occur or number of max sub-strings you can expect.

Example if I have string boo:and:foo and I will control number of splits to 2 like str.split(“:”,2), I will get 2 substrings, “boo” and “and:foo”.

Read more – http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#split%28java.lang.String,%20int%29

Dust me- clean up unused CSS

Recently came accross a legacy code which had a lot of css code and files, which was written over years and most of it was obsolete. Dust-me, firefox plugin helped to figure out all the unused css code. Though I had to traverse all possible flows in the application, but end result was good as few files were identified which were not being used completely and were removed, saving page load time.

Forgot your Tortoise SVN Password?

It happen to me a couple of times. Basically in Tortoise svn, you just need to add the password once and save it, it will keep working for months or years. In case I need to checkout another repository and it would ask the password again, it would be a nightmare.

But this simple tool helped- http://www.leapbeyond.com/ric/TSvnPD/

Download the exe file and execute it, it will pull out all the passwords saved for SVN.

Blue eyed boy- puzzle

This is conversation between two friends Ram and Shyam

Ram: Hey Bro, long time! How are you?
Shyam: I am good, How are you?
Ram: How are three boys, they must have grown up now. How old are they?
Shyam: Hmm, let me put it like this, if you multiply their ages, you will get the product as 36. And if you add their ages, you will get my house number.
Ram: Interesting, give me some more hint?
Shyam: Ok, my oldest son has blue eyes.
Ram: Awesome, so their ages are …

Puzzle: So what is the age of three boys?

———————————————————————

There are three hints

1. Product of the ages is 36.
2. Sum of the ages is same as house number. Interesting, we do not know house number, we will see how to use it.
3. Oldest is blue eyed boy. How is that useful?

Ok, lets start

1. Product of 3 ages is 36, so possible solutions are

1, 1, 36= 38(sum)
1, 2, 18= 21
1, 3, 12=16
1, 4, 9=14
1, 6, 6=13 *
2, 2, 9=13 *
2, 3, 6=11
3, 3, 4=10

2. Sum of the ages is house number
Looking at the above numbers, its clear that all the sums except 13 are unique, so if the house number was anything other than 13, Ram would have answered easily. But as he asked for one more hint, we now know that the house number is 13 and we are left with 2 possible solutions.
1, 6, 6=13 *
2, 2, 9=13 *

3. Oldest boy has blue eyes.
Out of the two options mentioned above, option 2 has a clear cut oldest age, i.e. 2, 2, 9. In case of 1, 6, 6, statement should have been one of the older boys or both old boys.

So ages are – 2, 2, 9

Tech Magic

I remember I watched first movie of Harry Potter series more than 10 years back. I was dazzled by the magic in the movie. Ofcourse I knew it was not real and not possible in real world. For example, I remember one scene which showed newspaper with moving images (don’t remember if that had voice too), and I thought, man that is impossible, how can a video is played over paper.

Shift ten years, a couple of months back I used Times of India alive app on my android phone, which can bring the images on newspaper alive (I created something similar myself using qualcomm’s augmented reality library 🙂 ). Well, it is not exactly same as newspaper playing the video, but is close enough, the video plays on your mobile, but it is triggered by newspaper images. Currently you have click scan button once you take your phone on top of newspaper, but I am sure there is lot of scope for improvement.

My point here is that what used to look like impossible has been rapidly transformed to mundane with use of technology. This is the most exciting time to be alive.

 

Web Service vs Web Application

There are times when I get confused if a web application is sufficient to solve a problem or I should be creating a web service. So I tried to get into depth of the two approaches.

What is a web application? web + application. A software application is something that is helping a user to achieve some specific goal, like MS Word, Calculator, Email Client etc. A web application, similarly, is an application which is available over a network or internet, it might be online banking application, some e-commerce application etc.

What is a web service? A service is similar to application in a sense that it is achieving a goal, but instead of being independent application, a service is providing inputs to some other application. For example, a service to add up two numbers, will take 2 numbers from a calculator app, add up two numbers, and return back to the calling applications.

Now how to take up the question if I should be creating a web application for a particular problem or web service. Let’s go back to calculator app, which needs a functionality to add 2 numbers. Web application approach will be to implement the add function inside the app, which is good enough if I know that this function is to be used only with this app. Web service approach will need the sum service to be independent of any application. This gives advantage that the same service can be used by multiple applications, this includes web apps, mobile apps or desktop apps as they will just need to connect to network and call the service to fulfill a requirement.

Above, we have defined web app and web service from a functional aspect. Technically, web service is a specialized web application only. A rule of thumb states that if your requirements want a user interface, you will need a web application implementation, but if your requirement does not need a interface but will return only some data, a web service is a good fit.