SortingAlgorithm

This commit is contained in:
edoardocoli
2024-03-03 23:03:44 +00:00
parent 62d7568c80
commit 4ce77085f7
376 changed files with 1612 additions and 9635 deletions
+65
View File
@@ -0,0 +1,65 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/time.h>
//The number of numbers that populate the file
#define TEST_SIZE 1
#define BUF_SIZE 2097152//16 MegaBytes
#define BUF_SIZE_HGB 67108864//512 MegaBytes
#define BUF_SIZE_GB 134217728//1 GigaBytes
#define BUF_SIZE_10GB 1342177280//10 GigaBytes
/*
Generate a file of given size filling it with random 8 bytes numbers.
Return the number of microseconds needed for the generation and writing on disk.
*/
long long benchmark_generate_file(const char *pathname, unsigned int seed)
{
struct timeval start, end;
unsigned int size = 0;
FILE *file;
int64_t *buffer;
srand(seed);
// if (access(pathname, F_OK) == 0)
// return -2;//File already exist
if (size == 0)
{
printf("Insert a multiple of %d(%d MegaBytes) for the size of the target file:\n", BUF_SIZE_GB, BUF_SIZE_GB/131072);
while (1)
{
if (scanf("%u", &size) != 1)
{
printf("Insert a valid size for the file:\n");
while (getchar() != '\n');//Clear the input buffer to prevent an infinite loop
}
else
break;
}
printf("Future file dimension: (%d * %u) Mb\n",BUF_SIZE_GB/131072, size);
}
buffer = (int64_t*)malloc(BUF_SIZE_GB * sizeof(int64_t));
if (!buffer)
return -1;//Something went wrong
file = fopen(pathname, "wb");
if (!file){
free(buffer);
return -1;//Something went wrong
}
gettimeofday(&start, NULL);//Timer Start
for (unsigned int i = 0; i < size; i++)
{
for(unsigned int j=0; j < BUF_SIZE_GB; j++)
{
buffer[j] = ((int64_t)rand() << 32) | rand();
// printf("%ld\n", buffer[j]);
}
fwrite(buffer, sizeof(int64_t), BUF_SIZE_GB, file);
}
gettimeofday(&end, NULL);//Timer Stop
free(buffer);
fclose(file);
return (end.tv_sec - start.tv_sec) * 1000000LL + (end.tv_usec - start.tv_usec);
}
+20
View File
@@ -0,0 +1,20 @@
#include <stdio.h>
long long benchmark_generate_file(const char *pathname, unsigned int seed);
long long benchmark_reader_file(const char *pathname);
int is_sorted(const char *pathname);
void print_partial_file(const char *pathname, unsigned long long startOffset, unsigned long long endOffset);
void print_all_file(const char *pathname);
int main(int argc, char* argv[]){
printf("scrittura file %s: time(%lld microseconds)\n","testiamolo.bin",benchmark_generate_file("testiamolo.bin",42));//TODO cambiare il seed una volta completatp
// printf("POI\n");
// printf("lettura file %s: time(%lld microseconds)\n","testiamolo.bin",benchmark_reader_file("testiamolo.bin"));
// printf("POI\n");
// printf("sono uguali? %d\n",is_sorted("testiamolo.bin"));
// print_partial_file("testiamolo.bin",2,4);
// printf("POI\n");
// print_all_file("testiamolo.bin");
return 0;
}
+107
View File
@@ -0,0 +1,107 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/time.h>
/*
*/
long long benchmark_reader_file(const char *pathname)
{
struct timeval start, end;
unsigned long long startOffset = 0; // Start from the first number (0-based index)
unsigned long long endOffset = -1; // Read up to the X number (exclusive), -1 in the last one because is unsigned
FILE *file;
int64_t num;
file = fopen(pathname, "rb");
if (!file)
return -1;//Something went wrong
fseek(file, startOffset * sizeof(int64_t), SEEK_SET);
gettimeofday(&start, NULL);//Timer Start
unsigned long long currentOffset = startOffset;
while (currentOffset < endOffset && fread(&num, sizeof(int64_t), 1, file) == 1)
currentOffset++;
gettimeofday(&end, NULL);//Timer Stop
fclose(file);
return (end.tv_sec - start.tv_sec) * 1000000LL + (end.tv_usec - start.tv_usec);
}
/*
*/
int is_sorted(const char *pathname)
{
unsigned long long startOffset = 0; // Start from the first number (0-based index)
unsigned long long endOffset = -1; // Read up to the X number (exclusive), -1 in the last one because is unsigned
FILE *file;
int64_t num;
long long int count=1;
file = fopen(pathname, "rb");
if (!file)
return -1;//Something went wrong
fseek(file, startOffset * sizeof(int64_t), SEEK_SET);
unsigned long long currentOffset = startOffset;
int64_t tmp;
fread(&tmp, sizeof(int64_t), 1, file); // Take first element(number) in the file
currentOffset++;
while (currentOffset < endOffset && fread(&num, sizeof(int64_t), 1, file) == 1)
{
count++;
if(tmp > num){
fclose(file);
// printf("non ordinati\n");
return 0;
}
tmp = num;
currentOffset++;
}
fclose(file);
// printf("%lld numeri ordinati",count);
return 1;
}
/*
Start from the start number (0-based index), read up to the end number (exclusive).
*/
void print_partial_file(const char *pathname, unsigned long long startOffset, unsigned long long endOffset)
{
FILE *file;
int64_t num;
file = fopen(pathname, "rb");
if (!file)
return;//Something went wrong
fseek(file, startOffset * sizeof(int64_t), SEEK_SET);
unsigned long long currentOffset = startOffset;
while (currentOffset < endOffset && fread(&num, sizeof(int64_t), 1, file) == 1)
{
printf("%lld\n", (long long)num);
currentOffset++;
}
fclose(file);
return;
}
/*
*/
void print_all_file(const char *pathname)
{
unsigned long long startOffset = 0; // Start from the first number (0-based index)
unsigned long long endOffset = -1; // Read up to the X number (exclusive), -1 in the last one because is unsigned
FILE *file;
int64_t num;
file = fopen(pathname, "rb");
if (!file)
return;//Something went wrong
fseek(file, startOffset * sizeof(int64_t), SEEK_SET);
unsigned long long currentOffset = startOffset;
while (currentOffset < endOffset && fread(&num, sizeof(int64_t), 1, file) == 1)
{
printf("%lld - ", (long long)num);
currentOffset++;
}
printf("EOF\n");
fclose(file);
return;
}