При сортировке qsort-ом по полю типа char почемуто во много раз увеличевается количество операций ...
наверно ето осособенность алгоритма??? или у меня где то ошибка искал вроде — нету может из за ограничеого круга значений хотя как то сомнительно...
Что можите посоветовать чтобы всётаки увеличить скорость сортировки ...
#include "stdafx.h"
#include <stdlib.h>
#include <memory.h>
#include <conio.h>
#include "Windows.h"
struct r1 {
short p3;
char p4;
int p5;
};
struct r2 {
int p3;
int p4;
int p5;
};
union u1 {
struct r1 rr1;
struct r2 rr2;
};
struct st {
int p1;
int p2;
int level;
union u1 uu1;
};
#define NUM 100000
int random_fill(struct st *sst) {
for(int i = 0 ; i < NUM; i++) {
int temp = rand();
memcpy(&sst[i].p1, &temp, sizeof(sst[i].p1));
temp = rand();
memcpy(&sst[i].p2, &temp, sizeof(sst[i].p2));
temp = rand();
memcpy(&sst[i].uu1.rr1.p3, &temp, sizeof(sst[i].uu1.rr1.p3));
temp = rand();
memcpy(&sst[i].uu1.rr1.p4, &temp, sizeof(sst[i].uu1.rr1.p4));
temp = rand();
memcpy(&sst[i].uu1.rr1.p5, &temp, sizeof(sst[i].uu1.rr1.p5));
}//for
return 1;
}
unsigned int colsort;
unsigned int kol;
int compare(const void *s1, const void *s2) {
kol++;
struct st *p1 = (struct st *)s1;
struct st *p2 = (struct st *)s2;
switch(colsort) {
case 1:
return p1->p1 - p2->p1;
case 2:
return p1->p2 - p2->p2;
case 3:
return p1->uu1.rr1.p3 - p2->uu1.rr1.p3;
case 4:
return p1->uu1.rr1.p4 - p2->uu1.rr1.p4;
case 5:
return p1->uu1.rr1.p5 - p2->uu1.rr1.p5;
default:
return 0;
}
}
void printtime(int tb, int te){
printf("Col %u Time %u Operation %u\n", colsort, te - tb, kol);
}
int main(int argc, char* argv[])
{
struct st *sst = new struct st[NUM];
random_fill( sst );
unsigned int timeb, timee;
colsort = 1;
for ( int i = 0 ; i < 6; i ++) {
timeb = GetTickCount();
qsort( sst, NUM, sizeof(struct st), compare);
timee = GetTickCount();
printtime(timeb, timee);
colsort++;
kol = 0;
random_fill( sst );
}
getch();
return 0;
}