Showing posts with label Programming. Show all posts
Showing posts with label Programming. Show all posts

Saturday, August 02, 2014

A simple Rust `pow` function for the built-in numeric types


While playing with Rust I wrote a simple pow function:

fn pow<T: Num + FromPrimitive + Copy>(a: T, mut b: uint) -> T {
    if b == 0 {
        return FromPrimitive::from_int(1)
            .expect("1 must be convertible to type of a");
    }
    let mut m: T = a;
    b -= 1;
    while b != 0 {
        m = m * a;
        b -= 1;
    }
    return m;
}

Here is a link to the playpen where you can test it out: http://is.gd/JBWtVf

Thursday, November 10, 2011

C/C++ going stronger than what people think

TIOBE published their programming language index for November, 2011 today and as per the usual trend Java, C and C++ are the top three programming languages. I am surprised to see that even after becoming old compared to many of the widely used programming languages, C and C++ are still holding their spots. I am particularly happy to see those three languages at the top because those are also my top three favorite languages. I would be happy to see Python and Ruby doing better. Here is the graph on long term trends on top ten programming languages from that site:


Link: http://www.tiobe.com/content/paperinfo/tpci/images/tpci_trends.png

Wednesday, November 09, 2011

Development model using Git branching

I just read the awesome post on a Git branching model for software development. Thanks Naseem bhai for sharing the link with me. Git is my most favorite SCM tool. I think I am going to use the model for all personal projects. The author of the blog made his points by providing very clear examples. It is surely worthwhile to read it.

Thursday, October 20, 2011

Quick and easy way to split lots of strings in Java

Recently, I had to split strings in a method which would be called potentially couple of billion times. The strings were delimited by only one character e.g. '#'. To be as efficient as possible, I just used String.indexOf(delimiter) and then String.substring(i, j) to get the splits. For this case, I think this certainly is the most efficient solution because the String.substring() method does not allocate new memory, it just returns a new String object which has some meta information to get the string from the original string's buffer (this is a nice optimization but it has its own pitfall). I was happy with it but the code looked messy with all those String.indexOf() calls and lots of index maintenance. Upon getting a review from a colleague I looked for ways to make it look better and easier to maintain. I found couple of alternatives to do the this:

1. StringTokenizer
2. Scanner
3. String.split

StringTokenizer had to be discarded immediately as it is getting old and is only there for backward compatibility. The Javadoc itself says, "StringTokenizer is a legacy class that is retained for compatibility reasons although its use is discouraged in new code." The Scanner is a great utility but seems to be bit heavy for that method. I had to keep in mind that the method would be called potentially billions of times in a row and it has to be as quick as possible. This left me with only the last option. String.split works in a simple way. It returns an array of strings. So, I could just use something like this:

String splits [] = str.split("#");
for (String split : splits) {
    // Do something ...
}

However, as noted here, there is a performance issue with this. Each String.split() call compiles a regular expression pattern and then uses the pattern to split the string. The pattern compilation is indeed very expensive and can slow the method down significantly. So, essentially what the String.split() method does is this:

public String[] split(String regex) {
     return Pattern.compile(regex).split(this);
}

By looking at the implementation, it becomes immediately obvious what one can do if lots of strings need to be split by the same regular expression. One can just have a pre-compiled pattern and call its split method repeatedly:

Patter pattern = Pattern.compile(regex);
...
for (String str : strings) {
    String splits [] = pattern.split(str);
    // Do something with the splits ...
}


The only thing unknown to me is whether the returned strings allocate new memory or the Pattern.split() method just behaves as String.substring(). But from running the code with more than 2.5 billion strings, I did not see any performance difference between the old and the new approach. It might mean that copies of strings are actually not made.

Tuesday, July 26, 2011

Eclipse Color Themes Plugin

I was annoyed when I installed a color theme which I did not like. But there was no way to go back to the default theme because the theme made itself the default one! However, I came across the Eclipse Color Themes site. It has a good collection of decent themes. However, to try one you need to download the theme and then import and apply it. This is a painful process unless you install their cool color theme plugin. It is very easy to use and come pre-populated with the themes found in the site. You can import more and switch between the themes very easily. It is really worth giving a try.

Thursday, June 30, 2011

Navigation between Vim tabs

I like working with tabs in Vim. It is a quite handy feature. But the navigation between tabs is not easy. It requires to be in command mode and type commands. It is certainly not a quick way and specially if you are jumping between tabs frequently you will soon get tired typing the commands. So I mapped the F7 and F8 keys to go to previous and next tabs respectively. Basically one can map any two keys of his choice to do that. Here is what I added in my vimrc file:

map <F7> :tabp<Enter>
map <F8> :tabn<Enter>
imap <F7> <Esc>:tabp<Enter>
imap <F8> <Esc>:tabn<Enter>

I used both map and imap commands because one works in Ubuntu Linux but not when I use iTerm in Mac OS X and vice versa.