-
Notifications
You must be signed in to change notification settings - Fork 0
/
final.cpp
150 lines (121 loc) · 4.44 KB
/
final.cpp
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
145
146
147
148
149
150
// title: memset, OS homework
// author: Jiahong Ma
// date: 2020/12/03
// coding:UTF-8
#include <iostream>
#include <fstream>
#include <bitset>
#include <windows.h>
#define MAX_LENGTH 200000//内存替换的最大长度
#define LOG_LENGTH (MAX_LENGTH / 10)//每隔LOG_LENGTH输出一个log日志信号
#define CH 25//需要替换的字符
#define TIMES 30 //1e5//函数执行的次数
void mymemset(void *s, int ch, size_t n);
void originmemset(void *s, int ch, size_t n);
//进行实验,统计mymemset(),memset()所用时间
int main()
{
std::ofstream outfile;
outfile.open("result.txt");//将结果输出
char a[MAX_LENGTH];//被替换的内存空间的地址
//int a[MAX_LENGTH/4];
//时间统计相关函数
LARGE_INTEGER cpuFreq;
LARGE_INTEGER startTime;
LARGE_INTEGER endTime;
double runTime, perTime;
for (int length = 0; length < MAX_LENGTH; length++)
{
//-------------------------------------------------------
//mymemset(a, CH, length);
runTime = 0;
for (int i = 0; i < TIMES; i++)
{
QueryPerformanceFrequency(&cpuFreq);
QueryPerformanceCounter(&startTime);
mymemset(a, CH, length);
QueryPerformanceCounter(&endTime);
perTime = (((endTime.QuadPart - startTime.QuadPart) * 1000.0f) / cpuFreq.QuadPart);
runTime += perTime;
}
std::cout << "mymemset:(" << runTime / TIMES * 1e3 << ")us\t";
outfile << "mymemset:(" << runTime / TIMES * 1e3 << ")us\t";
//------------------------------------------------------
//-------------------------------------------------------
//originmemset(a, CH, length);
runTime = 0;
for (int i = 0; i < TIMES; i++)
{
QueryPerformanceFrequency(&cpuFreq);
QueryPerformanceCounter(&startTime);
originmemset(a, CH, length);
QueryPerformanceCounter(&endTime);
perTime = (((endTime.QuadPart - startTime.QuadPart) * 1000.0f) / cpuFreq.QuadPart);
runTime += perTime;
}
std::cout << "originmemset:(" << runTime / TIMES * 1e3 << ")us\n";
outfile << "originmemset:(" << runTime / TIMES * 1e3 << ")us\n";
//------------------------------------------------------
if (0 == length % LOG_LENGTH)
{
std::cout << "-----------------LOG:" << length / float(MAX_LENGTH) << "-----------------\n";
outfile << "-----------------LOG:" << length / float(MAX_LENGTH) << "-----------------\n";
}
}
outfile.close();
return 0;
}
void mymemset(void *s, int ch, size_t n)
{
//提前定义一些循环需要的变量并赋值
const short BitPerByte = 8;
size_t cnt = 0;
//为了确保函数的正确性,所有长度均由sizeof()得到,即便sizeof(unsigned long long)!=8
//函数也能正确执行
short len_ch = sizeof(unsigned char);
short len_ll = sizeof(unsigned long long);
unsigned char u_ch = (unsigned char)ch;
unsigned long long u_ll = (unsigned long long)u_ch;
unsigned char *schar = static_cast<unsigned char *>(s);
unsigned long long *sullong;
//将1Byte长度的usigned char拓展为8Byte长度的unsigned long long便于后续替换
for (int i = 1; i < len_ll / len_ch; i += len_ch)
{
u_ll += ((unsigned long long)u_ch << (i * BitPerByte));
//std::cout << (std::bitset<64>)u_ll << std::endl;
}
//对于当前地址不为8的倍数的数据,函数每次只替换1个Byte的数据
while ((unsigned long long)s % (unsigned long long)len_ll != 0 && cnt < n)
{
*schar++ = u_ch;
cnt++;
//std::cout << "conditionA\n";
}
sullong = (unsigned long long *)schar;
//对于当前地址为8的倍数的数据,函数每次替换8个Byte的数据,直到剩余待替换的长度小于8Byte
while (n - cnt >= len_ll)
{
*sullong++ = u_ll;
cnt += len_ll;
//std::cout << "conditionB\n";
}
schar = (unsigned char *)sullong;
//之后改为每次替换1个Byte的数据直到替换结束
while (cnt < n)
{
*schar++ = u_ch;
cnt++;
//std::cout << "conditionC\n";
}
}
void originmemset(void *s, int ch, size_t n)
{
size_t cnt = 0;
unsigned char u_ch = (unsigned char)ch;
unsigned char *schar = static_cast<unsigned char *>(s);
while (cnt < n)
{
*schar++ = u_ch;
cnt++;
}
}