struct timeval begin_time, end_time;
时间: 2024-05-19 18:13:40 浏览: 19
This code declares two variables of type "struct timeval": "begin_time" and "end_time". These variables can be used to measure the elapsed time between two points in a program. The "struct timeval" structure contains two members: "tv_sec" and "tv_usec", which represent the number of seconds and microseconds elapsed since the epoch (January 1, 1970).
相关问题
请从cache、函数调用开销、编译等方面优化下面的串行程序,测试每个优化措施的效果。用Linux环境,编译器不限(gcc,icc等都可以)。 #include <stdio.h> #include <stdlib.h> #include <math.h> #define M 1500 #define NM 2000 #define N 2500 void generate_matrix(double *A, long m, long n) { long i, j; for (i=0; i<m; i++) for (j=0; j<n; j++) { A[i*n + j] = rand()/(RAND_MAX+1.0); //0 - 1 A[i*n + j] = 2*A[i*n + j] - 1; //-1 - +1 } } double handle_data(double data) { return sqrt(fabs(data)); } void handle_matrix(double *A, double *B, double *C, long m, long nm, long n) { long i, j, k; double s; for (i=0; i<m; i++) { for (j=0; j<n; j++) { s = 0; for (k=0; k<nm; k++) s += A[i*nm + k] * B[k*n + j]; C[i*n + j] = handle_data(s); } } } double sum_matrix(double *A, long m, long n) { long i, j; double s = 0; for (i=0; i<m; i++) for (j=0; j<n; j++) s += A[i*n + j]; return s; } int main() { double *A = (double *)malloc(M * NM * sizeof(double)); double *B = (double *)malloc(NM * N * sizeof(double)); double *C = (double *)malloc(M * N * sizeof(double)); generate_matrix(A, M, NM); generate_matrix(B, NM, N); struct timeval begin_time, end_time; double run_time_ms; gettimeofday(&begin_time); handle_matrix(A, B, C, M, NM, N); gettimeofday(&end_time); run_time_ms = (end_time.tv_sec - begin_time.tv_sec)*1000 + (end_time.tv_usec - begin_time.tv_usec)*1.0/1000; printf("run_time = %lfms\n", run_time_ms); printf("Sum = %.4f\n", sum_matrix(C, M, N)); free(A); free(B); free(C); return 0; }
优化串行程序可以从以下几个方面入手:
1. Cache优化:尽量利用CPU缓存,减少缓存失效,提高程序运行效率。可以采用循环展开、数据对齐、局部变量优化等方法。
2. 函数调用开销优化:尽量减少函数调用,尤其是递归函数调用。可以采用inline函数、宏定义等方法。
3. 编译优化:使用合适的编译选项,如-O2、-O3等,进行代码优化。可以采用静态链接库、动态链接库等方式减少代码大小。
针对以上优化措施,可以分别进行测试,比较优化前后程序的性能指标,确定最佳优化方案。在Linux环境下,可以使用gcc、icc等编译器进行编译和测试。
本关的编程任务是补全右侧代码片段中Begin至End中间的代码,具体要求如下: 在 UartBlockRead 中,以阻塞的方式打开串口,并设置为5s的超时返回,然后从串口中读取数据,并返回读取数据的长度。 在 UartUnblockRead 中,以非阻塞的方式打开串口,从串口中读取数据,并返回读取数据的长度。 本关涉及的代码文件UartBlockTest.c的代码框架如下: #include <stdio.h> #include <sys/types.h> #include <unistd.h> int UartBlockRead (int fd, char *rbuf) { /*********Begin*******/ /**********End********/ } int UartUnblockRead (int fd, char *rbuf, int length) { /*********Begin*******/ /**********End********/
UartBlockRead 函数的代码如下:
```
int UartBlockRead (int fd, char *rbuf) {
int len = 0;
fd_set rfds;
struct timeval tv;
FD_ZERO(&rfds);
FD_SET(fd, &rfds);
tv.tv_sec = 5;
tv.tv_usec = 0;
int retval = select(fd + 1, &rfds, NULL, NULL, &tv);
if (retval == -1) {
perror("select()");
} else if (retval) {
len = read(fd, rbuf, sizeof(rbuf));
} else {
printf("No data within 5 seconds.\n");
}
return len;
}
```
UartUnblockRead 函数的代码如下:
```
int UartUnblockRead (int fd, char *rbuf, int length) {
int len = 0;
fd_set rfds;
struct timeval tv;
FD_ZERO(&rfds);
FD_SET(fd, &rfds);
tv.tv_sec = 0;
tv.tv_usec = 0;
int retval = select(fd + 1, &rfds, NULL, NULL, &tv);
if (retval == -1) {
perror("select()");
} else if (retval) {
len = read(fd, rbuf, length);
}
return len;
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)