A
abracadabra
I am reading an old book - Programming Pearls 2nd edition recently. It
says, "Even though the general C++ program uses 50 times the memory
and CPU time of the specialized C program, it requires just half the
code and is much easier to extend to other problems." in a sorting
problem of the very first chapter.
I modified the codes in the answer part, ran it and found it is almost
the contrary case. The qsortints.c is the C code that uses the qsort
function defined in stdlib.h. The sortints.cpp uses STL sort. The
bitsort.c uses the method of bitmap in the book. To my surprise, the
bitmap method is slowest! The qsort method slower, the sort fastest.
It is totally different from what is said in the book!
I repeated the experiment using Visual Studio 2005 on a Windows XP
machine, and mingw32(gcc 4.2.0), the result is the same:
sort>qsort>bitmap.
Here goes the code.
/* qsortints.c -- Sort input set of integers using qsort */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define iMax 2000000
#define iSize 1000000
int myrand()
{
return rand()%32767;
}
int randint(int a, int b)
{
return a + ( 32767*myrand() + myrand()) % (b + 1 - a);
}
int intcomp(int *x, int *y)
{
return *x - *y;
}
int a[iMax];
int main()
{
int i, p, t;
clock_t t_begin, t_end;
srand((unsigned) time(NULL));
for (i = 0; i < iSize+2000; i++)
a = i;
t_begin = clock();
for (i = 0; i < iSize; i++)
{
p = randint(i, iSize-1);
t = a[p]; a[p] = a; a = t;
}
qsort(a, iSize, sizeof(int), intcomp);
t_end = clock();
printf("%f\n", (t_end - t_begin)/1.0/CLOCKS_PER_SEC);
/*system("pause");
for (i = 0; i < iSize; i++)
printf("%d ", a);*/
return 0;
}
/* Output is 1.270000 on my SuSE 10 with gcc -O3 */
/* End of qsortints.c */
/* sortints.cpp -- Sort input set of integers using STL set */
#include <iostream>
#include <ctime>
#include <algorithm>
using namespace std;
int myrand()
{
return rand()%32767;
}
int randint(int a, int b)
{
return a + ( 32767*myrand() + myrand()) % (b + 1 - a);
}
const int iMax = 2000000;
const int iSize = 1000000;
int a[iMax];
int main()
{
int i, p, t;
clock_t t_begin, t_end;
srand((unsigned) time(NULL));
for (i = 0; i < iSize+2000; i++)
a = i;
t_begin = clock();
for (i = 0; i < iSize; i++)
{
p = randint(i, iSize-1);
t = a[p]; a[p] = a; a = t;
}
sort(a, a+iSize-1);
t_end = clock();
cout << (t_end - t_begin)/1.0/CLOCKS_PER_SEC << endl;
/*system("pause");
for (i = 0; i < iSize; i++)
cout << a << " ";*/
}
/* Output is 0.38 on SuSE */
/* End of sortints.cpp */
/* bitsort.c Sort input set of integers using bitmap */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define BITSPERWORD 32
#define SHIFT 5
#define MASK 0x1F
#define N 10000000
int a[1 + N/BITSPERWORD];
void set(int i)
{
a[i>>SHIFT] |= (1<<(i & MASK));
}
void clr(int i)
{
a[i>>SHIFT] &= ~(1<<(i & MASK));
}
int test(int i)
{
return a[i>>SHIFT] & (1<<(i & MASK));
}
int myrand()
{
return rand()%32767;
}
int randint(int a, int b)
{
return a+(32767*myrand()+myrand())%(b+1-a);
}
int main()
{
int i;
clock_t t_begin, t_end;
srand((unsigned)time(NULL));
for (i = 0; i < N; i++)
clr(i);
t_begin = clock();
for (i = 0; i< N-2000; i++)
set(randint(i, N-1));
t_end = clock();
printf("%f\n", (t_end - t_begin)/1.0/CLOCKS_PER_SEC);
/*for (i = 0; i < N; i++)
if (test(i))
printf("%d\n", i);*/
return 0;
}
/* Output is 1.45000 on SuSE */
/* End of bitsort.c */
Any suggestions?
says, "Even though the general C++ program uses 50 times the memory
and CPU time of the specialized C program, it requires just half the
code and is much easier to extend to other problems." in a sorting
problem of the very first chapter.
I modified the codes in the answer part, ran it and found it is almost
the contrary case. The qsortints.c is the C code that uses the qsort
function defined in stdlib.h. The sortints.cpp uses STL sort. The
bitsort.c uses the method of bitmap in the book. To my surprise, the
bitmap method is slowest! The qsort method slower, the sort fastest.
It is totally different from what is said in the book!
I repeated the experiment using Visual Studio 2005 on a Windows XP
machine, and mingw32(gcc 4.2.0), the result is the same:
sort>qsort>bitmap.
Here goes the code.
/* qsortints.c -- Sort input set of integers using qsort */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define iMax 2000000
#define iSize 1000000
int myrand()
{
return rand()%32767;
}
int randint(int a, int b)
{
return a + ( 32767*myrand() + myrand()) % (b + 1 - a);
}
int intcomp(int *x, int *y)
{
return *x - *y;
}
int a[iMax];
int main()
{
int i, p, t;
clock_t t_begin, t_end;
srand((unsigned) time(NULL));
for (i = 0; i < iSize+2000; i++)
a = i;
t_begin = clock();
for (i = 0; i < iSize; i++)
{
p = randint(i, iSize-1);
t = a[p]; a[p] = a; a = t;
}
qsort(a, iSize, sizeof(int), intcomp);
t_end = clock();
printf("%f\n", (t_end - t_begin)/1.0/CLOCKS_PER_SEC);
/*system("pause");
for (i = 0; i < iSize; i++)
printf("%d ", a);*/
return 0;
}
/* Output is 1.270000 on my SuSE 10 with gcc -O3 */
/* End of qsortints.c */
/* sortints.cpp -- Sort input set of integers using STL set */
#include <iostream>
#include <ctime>
#include <algorithm>
using namespace std;
int myrand()
{
return rand()%32767;
}
int randint(int a, int b)
{
return a + ( 32767*myrand() + myrand()) % (b + 1 - a);
}
const int iMax = 2000000;
const int iSize = 1000000;
int a[iMax];
int main()
{
int i, p, t;
clock_t t_begin, t_end;
srand((unsigned) time(NULL));
for (i = 0; i < iSize+2000; i++)
a = i;
t_begin = clock();
for (i = 0; i < iSize; i++)
{
p = randint(i, iSize-1);
t = a[p]; a[p] = a; a = t;
}
sort(a, a+iSize-1);
t_end = clock();
cout << (t_end - t_begin)/1.0/CLOCKS_PER_SEC << endl;
/*system("pause");
for (i = 0; i < iSize; i++)
cout << a << " ";*/
}
/* Output is 0.38 on SuSE */
/* End of sortints.cpp */
/* bitsort.c Sort input set of integers using bitmap */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define BITSPERWORD 32
#define SHIFT 5
#define MASK 0x1F
#define N 10000000
int a[1 + N/BITSPERWORD];
void set(int i)
{
a[i>>SHIFT] |= (1<<(i & MASK));
}
void clr(int i)
{
a[i>>SHIFT] &= ~(1<<(i & MASK));
}
int test(int i)
{
return a[i>>SHIFT] & (1<<(i & MASK));
}
int myrand()
{
return rand()%32767;
}
int randint(int a, int b)
{
return a+(32767*myrand()+myrand())%(b+1-a);
}
int main()
{
int i;
clock_t t_begin, t_end;
srand((unsigned)time(NULL));
for (i = 0; i < N; i++)
clr(i);
t_begin = clock();
for (i = 0; i< N-2000; i++)
set(randint(i, N-1));
t_end = clock();
printf("%f\n", (t_end - t_begin)/1.0/CLOCKS_PER_SEC);
/*for (i = 0; i < N; i++)
if (test(i))
printf("%d\n", i);*/
return 0;
}
/* Output is 1.45000 on SuSE */
/* End of bitsort.c */
Any suggestions?