// From the software distribution accompanying the textbook // "A Practical Introduction to Data Structures and Algorithm Analysis, // Third Edition (C++)" by Clifford A. Shaffer. // Source code Copyright (C) 2007-2011 by Clifford A. Shaffer. // Test the efficiency of various implementations for storing and // manipulating 32 boolean values. Methods tested are arrays of ints, // shorts, chars, and one-bit boolean values. #include "book.h" // 32 one-bit values using the C++ bit-flag operators struct btype { int f0 : 1; int f1 : 1; int f2 : 1; int f3 : 1; int f4 : 1; int f5 : 1; int f6 : 1; int f7 : 1; int f8 : 1; int f9 : 1; int f10 : 1; int f11 : 1; int f12 : 1; int f13 : 1; int f14 : 1; int f15 : 1; int f16 : 1; int f17 : 1; int f18 : 1; int f19 : 1; int f20 : 1; int f21 : 1; int f22 : 1; int f23 : 1; int f24 : 1; int f25 : 1; int f26 : 1; int f27 : 1; int f28 : 1; int f29 : 1; int f30 : 1; int f31 : 1; }; // An array of characters struct cstruct { char pos[32]; }; // An array of shorts struct sstruct { short pos[32]; }; // An array of longs (which is the same as an int on a 32-bit machine) struct lstruct { long pos[32]; }; // Now, lets see how long it takes to use these implementations int main(int argc, char** argv) { struct cstruct cs; // chars struct sstruct ss; // shorts struct lstruct ls; // longs struct btype b; // bits char csa[32]; short ssa[32]; long lsa[32]; long count; long i; // For timing, we can vary the number of iterations run Assert(argc == 2, "Usage: flags "); count = atol(argv[1]); cs.pos[0] = true; ss.pos[0] = true; ls.pos[0] = true; csa[0] = true; ssa[0] = true; lsa[0] = true; b.f0 = true; // For each interation, we are going to test the time to read and write // the values by doing a copy from one array position to another. // To cut down on the overhead of the outer loop, we do 120 assignements // within the loop body. Settime(); for (i=0; i