Archive for June 2008
Writing my own algorithm
I always considered myself a not-so-bad programmer, but I never have written my own algorithm for anything. Not to mean that I always lifted my code from somewhere, but almost always I would have coded some known algorithm to do the same work. You have pseudo codes in most algorithm text books.
Today, perhaps second or third time, I wrote my own algorithm to do a simple job: bit reverse an integer. It is a common thing in FFT implementations. In most cases, the bit reversal in FFT will use the block nature of FFT also. But my requirement is not in FFT. I looked around a bit and got quite a couple of links with working code. I ain’t linking them here.
Here is the snippet of code I wrote for this. I am not claiming anything by writing this code. Just that I spent a couple of minutes arriving at it. Its totally intuitive and “naive”, if you want to call it that.
int bitreverse(int val, int len) {
int i;
int br = 0;
for(i=0;i<len;i++) {
br += ((val&1) * ((int)exp2f((float)(len-i-1))));
val >>=1;
}
return br;
}
From python to C(pp)
Image via Wikipedia
Yesterday was big disappointment. My python code was taking just too long to process a graph of about 30000 nodes and 80000 edges. Actually, it was taking more than an hour to just construct it. After that I have a bunch of algorithms to run on it. And this graph is generated from a sample graph of about 1024 nodes and edges. But in reality I need to run the whole thing against an initial graph of 20000 nodes. The whole thing just doesn’t sound to work. There are two possibilities: First is my code could be dirty; Or python does not do my kind of work very well. I can’t say anything about python until I have cleared my code to be the best. But I don’t have the time to invest here, since all this is just paths to my actual work, and my work is elsewhere!
So, the other choice is to code the whole thing in C/C++. Actually it should be C, because I am not a particular fan on C++. But given the time constraints, I just may have to go with C++. The reason is simple. I want something like STL to hasten my job and not worry about performance. I simply don’t see any such library for C. There are some ways to use STL with C, I guess. So, C++ it is. I should probably spend some time and write STL like library for C or at least spend some time to see if somebody is already doing the job. Anybody knows some good library for C, please leave a comment. Yeah, and don’t bother asking why I am not a fan of C++. I wouldn’t bother to reply.

