111 220 chapter 5 optimizing program performance again

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ilar to the array representations used in many other languages, including Java. Bounds checking reduces the chances of program error, but, as we will see, significantly affects program performance. As an optimization example, consider the code shown in Figure 5.5, which combines all of the elements in a vector into a single value according to some operation. By using different definitions of compile-time constants IDENT and OPER, the code can be recompiled to perform different operations on the data. In particular, using the declarations 210 CHAPTER 5. OPTIMIZING PROGRAM PERFORMANCE code/opt/vec.c 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 /* Create vector of specified length */ vec_ptr new_vec(int len) { /* allocate header structure */ vec_ptr result = (vec_ptr) malloc(sizeof(vec_rec)); if (!result) return NULL; /* Couldn’t allocate storage */ result->len = len; /* Allocate array */ if (len > 0) { data_t *data = (dat...
View Full Document

Ask a homework question - tutors are online