《Code Complete》ch.25 代码调整策略

WHAT?

本章讨论程序性能调整问题。但是对用户来说,程序员按时交付软件,提供一个清爽的用户界面,避免系统经常死机常常比程序性能更加重要

WHY?

在程序设计这种文化中,编写出能够节省几微秒的代码可以证明你很酷--

HOW?

Pareto法则

即80/20法则,指你可以通过20%的努力获取80%的成果

一些无稽之谈

  • 搞基高级语言中,减少代码行数可以加快代码执行速度
  • 特定运算可能比其他的更快,代码规模也较小:在某个环境下提升程序性能的方法放到另一个环境中可能会损害程序性能。在调整代码的时候你实际上隐含了一个承诺,即你将根据环境变化(包括编译器种类、版本、库版本的改变等)重新进行剖测和调整
  • 应当随时随地进行优化
  • 程序的运行速度同其正确性一样重要:在程序无法正确运行时,不可能去要求程序应当更小巧或运行更快

常见的低效率之源

  • 输入/输出操作:程序效率底下的根源之一就是不必要的输入/输出,优先在内存中处理文件,而不是磁盘、数据库或者网络
  • 分页:引发操作系统缺页中断(page faults)的运算会比在同一页的运算慢很多
  • 系统调用:调用系统子程序的代价是非常可观的,通常涉及系统的上下文切换(context switch)——保存程序状态、恢复内核状态等
  • 解释型语言:慢于编译型语言
  • 错误:比如数据库缺少索引

性能测量

  • 如果没有测量性能变化,那么你想当然的优化结果不过是代码变得更加晦涩难懂
  • 应当用分配给程序的cpu时钟来计算,而不是日期时钟
  • 应当明确测量程序初始化带来的额外系统负担

转载于:https://www.cnblogs.com/maozhige/p/3826263.html

Certainly! Below is a complete code example based on the activities described in the document "Lab 6- Array and Pointer.pdf" from the University of Edinburgh's Engineering Software 3 course. This includes the `main.c` file for the `Pointer`, `Swap_values`, and `Sorting` applications. ### Pointer Application (`pointer_ex.c`) ```c #include <stdio.h> #include "platform.h" #include "xil_printf.h" int main() { init_platform(); // Declare a character pointer char *ptr; // Declare a character variable and initialize it char ch = 'A'; // Assign the address of ch to ptr ptr = &ch; // Print the original value of ch xil_printf("Original value of ch: %c\n", ch); // Change the value of ch using the pointer *ptr = 'B'; // Print the new value of ch xil_printf("New value of ch: %c\n", ch); cleanup_platform(); return 0; } ``` ### Swap Values Application (`swap_values.c`) ```c #include <stdio.h> #include "platform.h" #include "xil_printf.h" #include "sevenseg.h" void swap(int *a, int *b) { int temp = *a; *a = *b; *b = temp; } int main() { init_platform(); sevenseg_init(); // Declare two integer variables and initialize them int num1 = 10; int num2 = 20; // Print the original values xil_printf("Original values: num1 = %d, num2 = %d\n", num1, num2); // Call the swap function swap(&num1, &num2); // Print the new values xil_printf("Swapped values: num1 = %d, num2 = %d\n", num1, num2); // Display the swapped values on the 7-segment display while (1) { sevenseg_write(num1); xil_printf("Displaying num1: %d\n", num1); usleep(1000000); // Wait for 1 second sevenseg_write(num2); xil_printf("Displaying num2: %d\n", num2); usleep(1000000); // Wait for 1 second } cleanup_platform(); return 0; } ``` ### Sorting Application (`sorting.c`) ```c #include <stdio.h> #include "platform.h" #include "xil_printf.h" #include "sevenseg.h" #include "switches.h" void bubble_sort(int arr[], int n) { for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - i - 1; j++) { if (arr[j] > arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } } int main() { init_platform(); sevenseg_init(); switches_init(); // Define an array to hold the values from the slide switches int values[8]; // Get the values from the slide switches and store them in the array for (int i = 0; i < 8; i++) { values[i] = switches_read_bit(i); } // Print the original values xil_printf("Original values: "); for (int i = 0; i < 8; i++) { xil_printf("%d ", values[i]); } xil_printf("\n"); // Call the sorting function bubble_sort(values, 8); // Print the sorted values xil_printf("Sorted values: "); for (int i = 0; i < 8; i++) { xil_printf("%d ", values[i]); } xil_printf("\n"); // Display the sorted values on the 7-segment display while (1) { if (XGpio_DiscreteRead(&BTN, 1) == 0x01) { // Check if BTNL is pressed for (int i = 0; i < 8; i++) { sevenseg_write(values[i]); xil_printf("Displaying value: %d\n", values[i]); usleep(1000000); // Wait for 1 second } } } cleanup_platform(); return 0; } ``` ### Additional Notes: - **Platform Initialization**: Ensure that `init_platform()` and `cleanup_platform()` functions are correctly implemented to initialize and clean up the platform resources. - **Seven Segment Display**: The `sevenseg_init()` and `sevenseg_write()` functions should be implemented to control the 7-segment display. - **Switches**: The `switches_init()` and `switches_read_bit()` functions should be implemented to read the state of the slide switches. These code snippets should help you complete the tasks outlined in the lab document. Make sure to include the necessary header files and implement any additional functions required for your specific hardware setup.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值