1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
#include <iostream>
#include <math.h>
#include <iomanip>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <time.h>
#include <windows.h>
using namespace std;
// global and constants
const int n = 1000;
const int min = 0;
float *array = new float[n];
//konec konstant
//prototyps
void generate(float min, float max);
void vypis_pole(float pole[]);
void bubbleSort(float * array, int start, int konec);
DWORD WINAPI Thread(LPVOID args);
int how_ms(LPFILETIME pred, LPFILETIME po);
//end prototyps
HANDLE p1, p2, p3, p4;
FILETIME time_before1, time_after1;
FILETIME time_before2, time_after2;
FILETIME time_before3, time_after3;
FILETIME time_before4, time_after4;
struct arguments {
float*POLE; //array
int start; //start array
int end; //end array
};
int main(void)
{
generate(min, n);
arguments *arg1 = new arguments;
arguments *arg2 = new arguments;
arguments *arg3 = new arguments;
arguments *arg4 = new arguments;
int quarter = n / 4;
arg1->POLE = array;
arg1->start = 0;
arg1->end = quarter;
arg2->POLE = array;
arg2->start = quarter;
arg2->end = 2 * quarter;
arg3->POLE = array;
arg3->start = 2 * quarter;
arg3->end = 3 * quarter;
arg4->POLE = array;
arg4->start = 3 * quarter;
arg4->end = n;
GetSystemTimeAsFileTime(&time_before1);
p1 = CreateThread(0, 0, Thread, arg1, 0, 0);
p2 = CreateThread(0, 0, Thread, arg2, 0, 0);
p3 = CreateThread(0, 0, Thread, arg3, 0, 0);
p4 = CreateThread(0, 0, Thread, arg4, 0, 0);
WaitForSingleObject(p1, INFINITE);
WaitForSingleObject(p2, INFINITE);
WaitForSingleObject(p3, INFINITE);
WaitForSingleObject(p4, INFINITE);
GetSystemTimeAsFileTime(&time_after1);
vypis_pole(array);
printf("Time of sorts %d [ms]\n", how_ms(&time_before1, &time_after1));
cin.get();
return 0;
}
//function
void generate(float min, float max)
{
for (int i = 0; i < n; i++)
{
array[i] = (max - min) * ((((float)rand()) / (float)RAND_MAX)) + min;
}
}
void vypis_pole(float pole[])
{
for (int i = 0; i < n; i++)
printf("%d : %.3f\n", i, pole[i]);
}
void bubbleSort(float * array, int start, int end){
for (int i = start; i < end - 1; i++){
for (int j = 0; j< end - i - 1; j++){
if (array[j + 1] < array[j]){
float tmp = array[j + 1];
array[j + 1] = array[j];
array[j] = tmp;
}
}
}
}
DWORD WINAPI Thread(LPVOID args)
{
printf("Thread\n");
arguments * o = (arguments*)args;
bubbleSort(o->POLE, o->start, o->end);
return 0;
}
int how_ms(LPFILETIME before, LPFILETIME after)
{
hyper pred64b = before->dwHighDateTime;
pred64b = (pred64b << 32) | before->dwLowDateTime;
hyper po64b = after->dwHighDateTime;
po64b = (po64b << 32) | after->dwLowDateTime;
// konverze 100ns -> 1ms
return (int)((po64b - pred64b) / 10000);
}
//end function
|