Skip to content

Commit

Permalink
Add sort method test function
Browse files Browse the repository at this point in the history
The test iterates through all the patterns 1~8 available currently and
size 20 to 10,000. Two sorting methods are tested, Linux `list_sort()`
and Timsort. Execution time and comparison count are the two performance
metrics used to benchmark the sorting algorithm. The output log is
printed to `sort.log` with format:
[type] [pattern] [size] [time] [comparisons]
and is ready for process.
  • Loading branch information
millaker committed Mar 16, 2024
1 parent 2821a26 commit 7106d36
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions qtest.c
Original file line number Diff line number Diff line change
Expand Up @@ -1324,6 +1324,45 @@ static struct list_head *prep_data(int pattern, int size)
return temp;
}

/* The output stats will be written to sort.log
with format [Type] [pattern] [size] [time] [comparisons]
*/

static bool do_sorttest(int argc, char *argv[])
{
FILE *fd = fopen("sort.log", "w");
// Cycle thorugh all patterns
for (int pattern = RAND; pattern < WORSTCASE; pattern++) {
for (int size = 20; size < 100000; size++) {
for (int j = 0; j < 1; j++) {
unsigned long long cmp_count = 0;
unsigned long long tbegin, tend, telapse;
struct list_head *temp_q = prep_data(pattern, size);

tbegin = clock();
list_sort(&cmp_count, temp_q, &cmp, 0);
tend = clock();
telapse = tend - tbegin;
fprintf(fd, "Merge %d %d %lld %lld\n", pattern, size, telapse,
cmp_count);
q_free(temp_q);

cmp_count = 0;
temp_q = prep_data(pattern, size);
tbegin = clock();
timsort(&cmp_count, temp_q, &cmp);
tend = clock();
telapse = tend - tbegin;
fprintf(fd, "Tim %d %d %lld %lld\n", pattern, size, telapse,
cmp_count);
q_free(temp_q);
}
}
}
fclose(fd);
return true;
}

static void console_init()
{
ADD_COMMAND(new, "Create new queue", "");
Expand Down Expand Up @@ -1369,6 +1408,7 @@ static void console_init()
"Do shuffle N times repeatedly and dump result to file",
"[file] [N]");
ADD_COMMAND(linux_list_sort, "Do linux list_sort.c merge sort", "");
ADD_COMMAND(sorttest, "Do sorting test", "");
ADD_COMMAND(timsort, "Do Timsort", "");
add_param("length", &string_length, "Maximum length of displayed string",
NULL);
Expand Down

0 comments on commit 7106d36

Please sign in to comment.