Discover the power of algorithms

Complexities of algorithm, thier applicability. Optimization techniques associated with diffrent algos.

Discover Power of Blogging

What is Blogging , is it a Dream or Passion or Award or Making Money ?

Think Diffrent, be creative

Let's see how much conclusion one can draw from it. This will help testing your creativity.

Discover the power of technology

Technolgy, Programming, Optimization , Gadgets and more...

Discover the power of Blogging

Google widgets and gadgets.

Mar 18, 2010

Perception a boon or a killer ?

Perception is considered to be starting point of thinking. In most of the cases perception starts with our experiences we have from life.

If we look thinking steps it would be:-
a) We see, observe or feel.
b) We percepts i.e. start thinking and analyzing.
c) We conclude and take actions.


Most of us just ignore the step (b), which i want to highlight with examples and will see some steps to improve this.

Lets take an example of one short story showing perception role in real life:-
In Australia, the one dollar coin is much bigger than two dollar coin. One day, one big guy and his friend offered five-year old Johnny a choice between two coins: a one dollar coin and the small two dollar coin. He could take and keep whichever one he wanted.He picked the bigger, one dollar coin.His friends laughed and giggled.Whenever they wanted to tease Johnny they offered him again this choice of coins.He always took the bigger one.He never seemed to learn.
   One day an adult saw this and called Johnny over.He told Johnny that although the smaller coin was smaller it was actually worth more."I know that", said Johnny. The adult man just got stunned from his answer. Johnny added "If i had taken smaller one first time, then how often they will offer me again".

With the above example it's very clear that perception made by big guys for that small kid was absolutely wrong. And utilizing same perception kid actually got the benefit.This example reveals us that don't precept that you are the most intelligent and the "only one".Always keep in mind that you cant fool people all the time. So be very cautious in case you start perceiving things.

Lets take one more practical example.








This is a small story indicating how perception can change with time and circumstances.

Rishita joined college after stepping out of school world. She made few friends and Pinky was one of them. They soon became good friends and shared a small world together of friendship and trust with lots of fun and enjoyment. One day Rishita was not feeling well so she asked Pinky to submit her assignment on her behalf, as the assignment marks were to be added to final score.

She was relieved that her assignment will be submitted on time. But next day she came to know that her assignment was not submitted and even Pinky was absent.
Rishita felt cheated and was very annoyed over Pinky. More important was that Pinky didn't even contact her once. One week passed by and Pinky joined back but Rishita decided not to talk to her. She created a perception for her and acted accordingly. Pinky tried to contact her many times but all in vein.

Few months passed by. Rishita was talking to one of her classmate and when Pinky was mentioned, she suddenly came up with an abrupt remark on Pinky's credibility. Then her classmate asked the reason for such comment and Rishita told her everything. Then her classmate broke her perception by telling truth about Pinky's unavailability.The second side of story gave Rishita shock of her life.

The day Pinky was going to submit Rishika's submission, her younger brother had a bad road accident and she was occupied for nearly 2 weeks with her brother's recovery. This was the reason she couldn't even inform Rishita. when she joined back she tried to contact Rishita, but Rishita's perception deprived her of the best friend she had.

At this time she realized that her wrong perception due to a single incident and incomplete information cost her best friend she had.

This example reveals that perception if made wrong can be a killer.

 A perception taken at a time plays very important role in real life situation.
As we saw perception taken by big guys was totally different from kids perception.
To kid perception made by big guys was a boon to him. We also seen
perception can make or break friendship.

Lets take one more example to understand that perception is a boon or a killer?



An old man of ninety goes down to Hell. As he wonders around he sees a friend of his, about the same age, with the most beautiful young woman on his knee.
He ask, "Is this hell? You seem to be having a good time."
The friend replies, "it is indeed hell, i am the punishment for the young lady."

This humorous example shows that perception can vary from people to people and its very difficult to get the right one. If you apply logic then also its difficult to create right perception. Perception as we seen can be a boon or it can be killer at times. So, just be careful whenever you precept things, try to add more and more dimensions to your thinking process before taking an action.

Hope you enjoyed reading the post.

Keep Rocking,
-Tajendra



KISS optimization principle.

Process/Code optimization is all time burning issue for projects/products in Industry. If we search for optimization techniques we get lots of complex theory and lengthy algorithm which claims to give optimizations. But its not advisable to run after such solution at very first level. One need to understand the need for it and that's what KISS principle is all about.


The "keep it simple, stupid" (KISS) principle, calls for giving simplicity a high priority in development.
So one need to consider this at the time of development  the reason i think is "One can easily apply  optimization on simpler code instead of complex one."

It is very similar to a maxim from Albert Einstein's that states, "everything should be made as simple as possible, but no simpler.", the difficulty for many adopters have is to determine what level of simplicity should be maintained. In any case, analysis of basic and simpler system is always easier, removing complexity will also open the door for code reutilization and a more generic approach to tasks and problems.

 So, whenever you start development keep this "KISS"  principle in mind.

Some optimization Link from Amazon:-
Combinatorial Optimization (3 volume, A,B, & C)
Elements of Dynamic Optimization
Linear Programming with MATLAB (MPS-SIAM Series on Optimization)



Mar 10, 2010

C++ Brain Food : A must solve c++ question.

I really like this c++ question. It really helps us to understand exception handling mechanism of c++.

Lets check you will get it right or not:-

Don't just execute code with compiler to get the result, try out by doing dry run.

What output does this program generate as shown? Why?

#include

using namespace std;

class A {
public:
A() {
cout << "A::A()" << endl;
}
~A() {
cout << "A::~A()" << endl; throw "A::exception";
}
};

class B {
public:
B() {
cout << "B::B()" << endl; throw "B::exception";
}
~B() {
cout << "B::~B()";
}
};

int main(int, char**) {
try {
cout << "Entering try...catch block" << endl;

A objectA;
B objectB;

cout << "Exiting try...catch block" << endl;
} catch (const char* ex) {
cout << ex << endl;
}

return 0;
}






Mar 5, 2010

What is Inline function in c/c++? A discussion on its Advantages,Disadvantages,Performance and Uses Guidelines.

Inline function is the optimization technique used by the compilers. One can simply prepend inline keyword to function prototype to make a function inline. Inline function instruct compiler to insert complete body of the function wherever that function got used in code.

Advantages :-
1) It does not require function calling overhead.
2) It also save overhead of variables push/pop on the stack, while function calling.
3) It also save overhead of return call from a function.
4) It increases locality of reference by utilizing instruction cache.
5) After in-lining compiler can also apply intra-procedural optimization if specified. This is the most important one, in this way compiler can now focus on dead code elimination, can give more stress on branch prediction, induction variable elimination etc..



Disadvantages :-
1) May increase function size so that it may not fit on the cache, causing lots of cahce miss.
2) After in-lining function if variables number which are going to use register increases than they may create overhead on register variable resource utilization.
3) It may cause compilation overhead as if some body changes code inside inline function than all calling location will also be compiled.
4) If used in header file, it will make your header file size large and may also make it unreadable.
5) If somebody used too many inline function resultant in a larger code size than it may cause thrashing in memory. More and more number of page fault bringing down your program performance.
6) Its not useful for embedded system where large binary size is not preferred at all due to memory size constraints.








Performance : -
Now covering the topic which most the people are interested in the "Performance".
In most of the cases Inline function boost performance if used cautiously as it saves lots of overhead as discussed in our Advantages section above but as we have also discussed  its disadvantages one need to be very cautious while using them. Today's modern compiler inline functions automatically, so no need to specify explicitly in most of the cases. Although placing inline keyword only gives compiler a hint that this function can be optimized by doing in-lining, its ultimately compiler decision to make it inline. Though there are ways to instruct compiler too, for making a function call inline like one can use __forceinline to instruct compiler to inline a function while working with microsoft visual c++. I suggest not to use this keyword until you are very sure about performance gain. Making a function inline may or may not give you performance boost, it all depends on your code flows too. Don't expect a magical performance boost by prep-ending inline keyword before a function to your code as most of the compiler nowadays does that automatically.

As we have seen inline function serves in terms of performance but one has to use it with extreme cautions.

I have prepared a few guidelines for its use.
Uses Guidelines :-
1) Always use inline function when your are sure it will give performance.
   Recently i got very good example showing immature use of inline function.
Suppose you have used inline function and for some reason compiler rejects it.
Well, when the compiler cannot inline a function, it generates a static definition of the function instead. In other words, the code for the function is generated just like the code for a static function. This means that every translation unit in which an inline function cannot be inlined will get its own copy of the function, which can increase the size of your program. Most compilers will report warnings when a function cannot be inlined. You should pay attention to these warnings to determine if some functions should not be inlined in the first place.

There is one more detail with inline functions that you should be aware of. Suppose you have chosen to use a local static variable in an inline function:

inline void foo()
{
static int index;
// ...
}

Next, suppose for some reason, the compiler cannot inline this function in several translation units. Each translation unit will then have its own static definition of foo(). But, this means that each version of foo() may also have its own copy of the static variable index! Clearly not what you want. So avoid putting static variables inside of non-member inline functions.
2) Always prefer inline function over macros.
3) Don't inline function with larger code size, one should always inline small code size function to get performance.
4) If you want to inline a function in class, then prefer to use inline keyword outside the class with the function definition.
5) In c++, by default member function declared and defined within class get linlined. So no use to specify for such cases.
6) Your function will not be inlined in case there is differences between exception handling model. Like if caller function follows c++ structure handling and your inline function follows structured exception handling.
7) For recursive function most of the compiler would not do in-lining but microsoft visual c++ compiler provides a special pragma for it i.e. pragma inline_recursion(on) and once can also control its limit with pragma  inline_depth.
8) If the function is virtual and its called virtually then it would not be inlined. So take care for such cases, same hold true for the use of function pointers.


That's it from my side, I hope you enjoyed reading the post.



Mar 4, 2010

The unbalanced discovery boosts the unseen blast.

Lets see how much conclusion one can draw from this post topic.
This will help testing your creativity.Such random exercise help one's to open their mind, to let it fly in this big sky of imagination. Let's see how far you can fly :)



Mar 2, 2010

How to achieve code portability ? Basic 10 quick tips.

Code portability basically refers to making source code able to compile on different platform without making any changes in source code.
While coding its very important to keep portability in mind.The best way to introduce code portability is while coding.Keeping certain things into account we can achieve code portability with lesser effort, which we will discuss in this post.There are certain tools too which detect portability problems on source code, its a post processing of code and requires extra effort.
Non-portable code introduces problems like maintenance of different versions, reduces readability, reduces understanding of code etc...
Efforts needs to make legacy or old source code portable, can really make you feel lost in this big programming ocean. So, the best policy is to keep portability into account while writing code, it saves lots of time and efforts on rework. Big question now is - "How to write portable code?".Our source code should be compatible with different environment like different processor, different OS, different version of libraries etc... In this post we would focus on basic tips need to be kept in mind while writing code. 

1) Don't assume data type size to be constant across platform, as it may change with platform.
             Many a times programmers makes a common mistake by assuming size of pointer and long same.If in some expression sizeof(long) is used, it may give different result on 32-bit and 64-bit OS version. Like if we talk about Microsoft Visual Studio running on 64-bit OS version the pointer size would be 8 byte and size of long comes out to be 4 byte. Program written with such assumption would give false result or may even get crash.So, one has to be very cautious while using data type size across the platform.

2) Don't use specific system constants.
            System specific constant should not be used as they are not portable, we are some time not aware of them also. 

3) System file/folder path notation may vary on different platform.
           When working with file path one need to be cautious for example "\\testfolder\\TestFile.txt" will work on Windows but give error on Linux.For this one i recommend to use forward slash "/tesfolder/TestFile.txt" , it would work well on both windows and Linux.

4) Avoid using system specific models/libraries.
         Don't use system specific models/libraries like Event handling model, Threading libraries, File Creation libraries etc.. . As they are not compatible across platform. Write a wrapper around such models and within wrapper use generic portable libraries. For example, Windows even handling model is totally different from Linux. Windows have special mode for handling events, like we may not find timed wait for multiple object on other platform.

5) Always write default statement in switch case.
         Many latest compiler gives compilation error if default is not specified.

6) Always specify return type for functions. 
         Many latest compiler gives compilation error if return type is not specified.

7) Always specify type with static variables.
         Variables declared with static keyword must contain data type with it, some old compiler take int as default type but modern compiler will generate compilation error for it.

8) Always take care of scope of variable.
        Like some compiler support variable scope limited to for() while some compiler dont.
    For example:-
        Don't prefer writing code as below (Non-portable code).
        {
           for(int i ; ;)
          {
          //do some thing
           }


           for(int i ; ;)
         {
          //do some thing
          }

        }


         Prefer writing code as below (Portable code)
        {
           for(int i ; ;)
          {
          //do some thing
           }

           for(int j ; ;)
         {
          //do some thing
          }

        }

9) Don't use C++ commenting style in C code.
      Don't use // commenting style in c code, as compile other then microsoft visual studio may generate error for it. Prefer using /* */ commenting style.

10) Take care of include depth for header files and also for file code size.
       Microsoft visual studio compiler generated error like "internal compiler error"  if include depth is too large or file size exceeds certain limit. Always take care of file size and include depth. 

I have tried to cover 10 basic tips for code portability for beginners though there are several other areas too, where we need to focus on advanced portability issues, for e.g. dealing with classes, virtual functions, exception handling, compiler directives, run-time identification. I will cover this topic separately bye for now.
  
Hope you enjoyed this post !

Keep Rocking
-Tajendra



What's the difference amongst code portability, software portability and application poratoability ?

Many people gets confused of terms Code Portability, Software portability and Application portability.
Lets understand the differences amongst them. Application portability means that application is designed in a way that it can run independently without actually installing its file on the system. A portable application did not install any file on the system and can be run with the help of removable devices like USB, portable hard drive, CD, flash drive or floppy disk. Application portability is often get confused with Software portability, where as Software portability refers to designing of software in a way that its source code can be compiled on different platform without any changes. Software portability is also referred as code portability, both the terms can be used alternatively. Application portability does not implies that it can run on multi-platform, it only implies that it runs independently on system from its storage location only. Portable application saves or read its configuration from its storage location, it does not leave any files on the system its running upon. This feature make the application portable.
So now difference is very clear between software portability and application portability.

Hope you enjoyed the post !
-Tajendra



Mar 1, 2010

"Lathmar Holi" a traditional Indian Holi showing Radha's Sweet Festive Revenge

Today is Holi, the Indian festival of colors. One will not find as many color as found in this festival. Auspicious red, Soothing silver, Sunkissed gold, Pretty purple, Blissfull blue, Forever green all colors are there. People used to play holi by forgetting all thier previous rivalry and used to enjoy great indian cusine of sweets, typical indian "bhang",traditional drink called as "thandaii", play color names as "gulal". Its all very colorful and you can put color to anybody by just
saying "Bura na mano holi hai".
Holi get celebrated in many ways and today i have chosen to write on "lathmar holi" which is treated as epicenter of holi in India. I have chosen Lathmar holi because it is unique in the sense that here women chase men away with sticks. Males also sing provocative songs in a bid to invite the attention of women. Women then go on the offensive and use long staves called "lathis" to beat men folk who protect themselves with shields. Before moving ahead with it lets undestand the reason of Holi celebration there are several sagas behind it "Love of Lord Krishna-Radha", "Legend of Prahlad and Pootna" and "Legend of King Hiranyakashyapu". I like "Legend of Krishna and Radha" the most as it spread message of love.
Krishna being a very mischievous kid plays lots of pranks with Radha and her friends (Gopies). Yashuda son Krishna once asked her that "why Radha so fair and i am so dark", replying to him Mother said "If you are too much jealous of her than put dark color on Radha so there will be no color difference". Krishna did same by putting dark color on Radha. Since then people celebrates Holi by playing colors with his/her loved ones.
One can ask question then from where this Lathmar Holi came from, so answer is its from Radha's and Gopies.
Its a battle of love between Nandgaon's men (Lord Krishna's Place) and Barsana's women (Shri Radha's Place).
Lord Krishna was famous for palying pranks with Gopies of Vraj and Gopies too enjoy this by showing thier love anger over it. So, continuing this ritual Men and Women of Vraj even today clash in a colorful display of battle of the sexes. Men of Nandagow tries to invade Barsana with hopes of raising their flag over Shri Radhikaji's temple (Most famous temple dedicated only to Shri Radhika in India). In response Barsana women with thier full preparation attacks men with long wooden stick termed as "lathis" to take sweet festive revenge for pranks played by Lord Krishna.Slowly it turns up into a love battle where men attempt to reach Shri Radhikaji's temple defending themselves with shield.Funny part of this battle is if men gets up into hold of women then they are forced to dance in female attire too. This Love battle showing Radha's sweet festive revenge is Known as “Lathmar Holi”!! also famous as BRIJ KI HOLI.
One can rightly say here that "It happens only in India". Lets keep this divine love of Krishna and Radha alive deep in our hearts, so that we can spread message of love and happiness in this world of hatred

Happy Holi,