Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sort_test.c for Intel PT case #205

Merged
merged 1 commit into from
Apr 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion pt/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

CC ?= "${CC}" -g -Wall
CFLAGS += -DMAINLINE -I./
BIN = cpl branch psb nonroot_test negative_test
BIN = cpl branch psb nonroot_test negative_test sort_test
LFLAGS = -L./ -lipt


Expand All @@ -25,5 +25,8 @@ nonroot_test:
negative_test:
$(CC) -o $@ [email protected] utils.c ${CFLAGS} ${LFLAGS}

sort_test:
$(CC) -o $@ [email protected] utils.c ${CFLAGS} ${LFLAGS}

clean:
rm -rf $(BIN) *.o
73 changes: 73 additions & 0 deletions pt/sort_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// SPDX-License-Identifier: GPL-2.0-only
// Copyright (c) 2024 Intel Corporation.
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>

#define ARRAY_LEN 3000

void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}

size_t partition(int *a, size_t low, size_t high)
{
int pivot = a[high];
size_t i = low - 1;

for (size_t j = low; j <= high - 1; j++) {
if (a[j] < pivot) {
i++;
swap(&a[i], &a[j]);
}
}
swap(&a[i + 1], &a[high]);
return (i + 1);
}

static inline void start(struct timeval *tm)
{
gettimeofday(tm, NULL);
}

static inline void stop(struct timeval *tm1, struct timeval *tm2)
{
unsigned long long t = 1000 * (tm2->tv_sec - tm1->tv_sec) +
(tm2->tv_usec - tm1->tv_usec) / 1000;
printf("%llu ms\n", t);
}

void quick_sort(int *a, size_t low, size_t high)
{
if (low < high) {
size_t pivot = partition(a, low, high);

quick_sort(a, low, pivot - 1);
quick_sort(a, pivot + 1, high);
}
}

void sort_array(void)
{
printf("Quick sorting array of %zu elements\n", ARRAY_LEN);
int data[ARRAY_LEN];

for (size_t i = 0; i < ARRAY_LEN; ++i)
data[i] = rand();

quick_sort(data, 0, ARRAY_LEN - 1);
}

int main(void)
{
struct timeval tm1, tm2;

start(&tm1);
sort_array();
gettimeofday(&tm2, NULL);
stop(&tm1, &tm2);
return 0;
}
Loading