我在对它们进行排序后加入两个文件,并评估我需要获得平均时间的时间。但根据我的编码,它会在几秒钟内显示,因此执行速度足以让结果在1秒内完成,因此无需获取平均值。这就是为什么我想以毫秒为单位打印时间,以便我可以采用这些时间并计算平均时间。这是我的代码 -
#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;
}
答案 0 :(得分:0)
使用clock()而不是time()。您应该能够将时间(NULL)更改为clock()。 clock_t也可以替换time_t。