정렬 한 후 두 파일을 결합하고 평균 시간을 얻는 데 필요한 타이밍을 평가합니다. 하지만 코딩에 따르면 초 단위로 표시되기 때문에 실행이 1 초 만에 결과를 얻을 정도로 빠르기 때문에 평균값을 얻는 데는 아무 것도 없습니다. 그래서 시간을 밀리 초 단위로 출력하여 평균치를 계산할 수 있습니다. 여기 내 코드가 있습니다 -C를 사용하여 시간을 밀리 초 단위로 표시하는 방법
#include <sys/stat.h> // S_IRUSR
#include <time.h> // time_t time
#include "mergesort.h"
typedef struct {
int fd; //file description ID
int type; //the schema type of file
page_t *page; //page buffer which use to store records
} file_t;
/*! \fn file_t *file_init(char *file_path, int type, int page_size)
* \brief initialize the file structure with inputted parameters
* \param file_path the path of need to handled file
* \param type the type of file.
* 1 means the file formated with characters schema
* 2 means the file formated with guilds schema
* \param page_size the size of page, the page will use to buffer the
* record which read from file
* \return not NULL the file structure
* NULL means cannot construct the file structure
*/
file_t *file_init(char *file_path, int type, page_t *page)
{
file_t *file = calloc(1, sizeof(file_t));
char sorted_file[1024] = {0};
snprintf(sorted_file, 1023, "%s.sorted", file_path);
file->fd = open(sorted_file, S_IRUSR);
file->type = type;
file->page = page;
return file;
}
int merge_sort(pass_t *pass, int page_size, int page_num, char *file)
{
int ret = OK;
if (pass->pages == NULL)
{
pass->pages = calloc(pass->page_num, sizeof(page_t));
}
// parse input
pass->page_size = page_size;
pass->page_num = page_num;
pass->input_file = file;
pass->output_file = "/tmp/merge_tmp";
pass->type = 1;
if (strstr(pass->input_file, "guilds") != NULL)
{
pass->type = 2;
}
// pass 0: split the original into sorted file pieces
pass_0(pass);
// pass n: merge the file pieces into a sorted unique file
return pass_n(pass);
}
/*! \fn int main(int argc, char *argv[])
* \brief the entry of program
* \param argc the number of input parameters
* \param argv the list of input parameters
* 1st is program name
* 2nd is the optional flag to indicate ouput matched record to screen
* 2nd or 3rd is the record size of page
* 3rd or 4th is the page size
* 4th or 5th is one file which need to be merged
* 5th or 6th is another file which need to be merged
* \return 0 indicate the operation is success
* other indicate the operation is failed
*/
int main(int argc, char *argv[])
{
if (argc < 4)
{
fprintf(stderr,
"Usage: bin/sortmerge [-o] <pagesize> <buffers> <path>\n");
exit(1);
}
int param_id = 1;
int out_flag = 0;
if (strcmp(argv[param_id], "-o") == 0)
{
out_flag = 1;
param_id++;
}
int page_size = atoi(argv[param_id++]);
int page_num = atoi(argv[param_id++]);
int total_buf = page_size * page_num;
int ret = OK;
int tuples_num = 0;
time_t start = time(NULL);
pass_t file1_pass = {0};
merge_sort(&file1_pass, page_size, page_num, argv[param_id++]);
pass_t file2_pass = {0};
// reuse the allocated buffer
file2_pass.pages = file1_pass.pages;
merge_sort(&file2_pass, page_size, page_num, argv[param_id++]);
// open sorted file
file_t *chars_file = NULL;
file_t *guilds_file = NULL;
if (strstr(file1_pass.input_file, "guilds") != NULL)
{
chars_file = file_init(file2_pass.input_file, 1, &file2_pass.pages[0]);
guilds_file = file_init(file1_pass.input_file, 2, &file1_pass.pages[1]);
}
else
{
chars_file = file_init(file1_pass.input_file, 1, &file1_pass.pages[0]);
guilds_file = file_init(file2_pass.input_file, 2, &file2_pass.pages[1]);
}
record_t *guild_record = NULL;
record_t *char_record = NULL;
PAGE_BEFORE(chars_file->page, page_size);
PAGE_BEFORE(guilds_file->page, page_size);
do
{
// load and popup record from guilds
page_load(guilds_file->fd, guilds_file->page, guilds_file->type,
guilds_file->type);
guild_record = page_pop_record(guilds_file->page);
if (guild_record == NULL)
{
//no record then exit
break;
}
do
{
// load popup record from characters
page_load(chars_file->fd, chars_file->page, chars_file->type,
chars_file->type);
char_record = page_pop_record(chars_file->page);
if (char_record != NULL
&& guild_record->guild_id == char_record->guild_id)
{
// records matched with guild_id
tuples_num++;
if (out_flag == 1)
{
// output matched record information
printf("%d,%s,%d,%d,%d,%s\n", char_record->guild_id,
char_record->info.character.cname,
char_record->info.character.team,
char_record->info.character.level,
char_record->info.character.cid,
guild_record->info.guild.gname);
}
}
// if not find bigger than guild tuple record,
// continuously popup record from characters
}
while (char_record != NULL
&& guild_record->guild_id >= char_record->guild_id);
}
while (guild_record != NULL && char_record != NULL);
printf("Number of tuples: %d\nTime: %d seconds\n",
tuples_num, (time(NULL) - start));
return 0;
}
C++ 11을 사용할 수 있습니까? – soandos
@soandos 코드에는 실제로 C++ 코드가 없습니다. 어떻게 생각해? :- 어쨌든,'gettimeofday'는 아마도 당신이 원하는 것일 것입니다. – Xarn
@Xarn 추측 컨데, C++로 태그가 붙어 있습니다. 사람들이 그 일을 할 때 항상 나를 혼란스럽게합니다. – soandos