After modifying my C code, (written originally for Windows and compiled under VS 2008), I ran it on Linux. To my surprise it is now at least 10 times slower than windows version.
Using Profiler tools I figured out that the following function is consuming most of the time spent in the application:
/* advance by n bits */
void Flush_Buffer(N)
int N;
{
int Incnt;
ld->Bfr <<= N;
Incnt = ld->Incnt -= N;
if (Incnt <= 24)
{
if (System_Stream_Flag && (ld->Rdptr >= ld->Rdmax-4))
{
do
{
if (ld->Rdptr >= ld->Rdmax)
Next_Packet();
ld->Bfr |= Get_Byte() << (24 - Incnt);
Incnt += 8;
}
while (Incnt <= 24);
}
else if (ld->Rdptr < ld->Rdbfr+2044)
{
do
{
ld->Bfr |= *ld->Rdptr++ << (24 - Incnt);
Incnt += 8;
}
while (Incnt <= 24);
}
else
{
do
{
if (ld->Rdptr >= ld->Rdbfr+2048)
Fill_Buffer();
ld->Bfr |= *ld->Rdptr++ << (24 - Incnt);
Incnt += 8;
}
while (Incnt <= 24);
}
ld->Incnt = Incnt;
}
}
This function was taking negligible time on windows. on Linux it is taking taking close to 14 sec. What wrong I have committed here?
There are no system calls here so this code section should be independent of OS specific calls and thus should run in identical time.
(My Guess: This function is being called multiple times, so may be the profiler is accumulating the time of all the calls too. In such a case I think one of the issues might be that the function is not getting its input parameter quickly as compared to windows case. )
What wrong I have committed here? Any guess?
Rgrds,
H
You can try to annotate all code paths in your code with counters. At the end of the program, each counter will contain information about how many times the code path has been executed. Comparing these numbers line-by-line between the Windows version and the Linux version may reveal that the program is following different code paths. Depending on the nature of the code paths, the differences may be able to explain why the Linux version is slower than the Windows version.