Feedback on ex0.* and ex1.*
This commit is contained in:
		
							parent
							
								
									e363d023cd
								
							
						
					
					
						commit
						982866a7e6
					
				
					 6 changed files with 47 additions and 21 deletions
				
			
		
							
								
								
									
										12
									
								
								ex0.2/gen_rand_explicit.py
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								ex0.2/gen_rand_explicit.py
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,12 @@ | |||
| #!/usr/bin/env python3 | ||||
| 
 | ||||
| import numpy as np | ||||
| import sys | ||||
| 
 | ||||
| N = 1e6 | ||||
| 
 | ||||
| if len(sys.argv) > 1: | ||||
|     N = float(sys.argv[1]) | ||||
| 
 | ||||
| for _ in range(int(N)): | ||||
|     print( np.random.rand(1) ) | ||||
|  | @ -1,4 +1,10 @@ | |||
| Running `time ./gen_rand.py "1e9"` gives  | ||||
| Running `time ./gen_rand.py "1e6"` gives  | ||||
| 
 | ||||
| real 0m13.803s | ||||
| user 0m8.874s | ||||
| real	0m0.237s | ||||
| user	0m0.160s | ||||
| 
 | ||||
| Contrast this to the following: | ||||
| Running `time ./gen_rand_explicit.py "1e6"` gives | ||||
| 
 | ||||
| real	2m11.564s | ||||
| user	2m1.950s | ||||
|  |  | |||
|  | @ -19,15 +19,15 @@ int main() { | |||
| 	unsigned int chars_capital = 0, chars_lower = 0, chars_digits = 0, chars_other = 0; | ||||
| 	while ( *ptr != 0 ) | ||||
| 	{ | ||||
| 		if ( *ptr >= int('A') && *ptr <= int('Z') ) | ||||
| 		if ( *ptr >= 'A' && *ptr <= 'Z' ) | ||||
| 		{ | ||||
| 			++chars_capital; | ||||
| 		} | ||||
| 		else if ( *ptr >= int('a') && *ptr <= int('z') ) | ||||
| 		else if ( *ptr >= 'a' && *ptr <= 'z' ) | ||||
| 		{ | ||||
| 			++chars_lower; | ||||
| 		} | ||||
| 		else if ( *ptr >= int('0') && *ptr <= int('9') ) | ||||
| 		else if ( *ptr >= '0' && *ptr <= '9' ) | ||||
| 		{ | ||||
| 			++chars_digits; | ||||
| 		} | ||||
|  |  | |||
|  | @ -39,7 +39,9 @@ char* join(const char* str1 , const char* str2) | |||
| 
 | ||||
| char* joinb(const char* str1 , const char* str2) | ||||
| { | ||||
| 	int size = strlen(str1) + strlen(str2) + 1; | ||||
| 	// size should be 2 more than required for the strlens to 
 | ||||
| 	// incorporate the space and the null byte
 | ||||
| 	int size = strlen(str1) + strlen(str2) + 2; | ||||
| 
 | ||||
| 	char* new_str = new char[size]; | ||||
| 	*new_str =  0; | ||||
|  |  | |||
|  | @ -15,12 +15,25 @@ | |||
| 
 | ||||
| #define MEMORY_SIZE_IN_CHARS G | ||||
| 
 | ||||
| void make_throwaway_ptrs(int leak_size ) { | ||||
| 	for ( int i = 0; i < leak_size ; i++) | ||||
| 	{ | ||||
| 		char* throwaway_ptr = new char; | ||||
| 	} | ||||
| 
 | ||||
| 	// oops, we are not returning the pointers
 | ||||
| 	// this means it goes out of scope when the function ends
 | ||||
| } | ||||
| 
 | ||||
| 
 | ||||
| int main() { | ||||
| 	char* throwaway_ptr = new char[MEMORY_SIZE_IN_CHARS]; | ||||
| 	make_throwaway_ptrs(MEMORY_SIZE_IN_CHARS); | ||||
| 
 | ||||
| 	std::cout << "We leaked " << MEMORY_SIZE_IN_CHARS * sizeof " " << " bits" << std::endl; | ||||
| 
 | ||||
| 
 | ||||
| 	std::cout << "Ready to kill the process? (press Enter)" << std::endl; | ||||
| 	std::cin.ignore(); | ||||
| 	return 0; | ||||
| } | ||||
| 	 | ||||
| 
 | ||||
|  |  | |||
|  | @ -23,10 +23,10 @@ | |||
| #include <iostream> | ||||
| #include <cmath> | ||||
| 
 | ||||
| #define VERBOSE false | ||||
| #define VERBOSE true | ||||
| 
 | ||||
| char b32_lookup_table[] = "ABCDEFGHIJKLMNOPQRSTUV"; | ||||
| int base = 8; | ||||
| char b32_lookup_table[] = "0123456789ABCDEFGHIJKLMNOPQRSTUV"; | ||||
| int base = 32; | ||||
| 
 | ||||
| int main() { | ||||
| 	//a
 | ||||
|  | @ -75,7 +75,7 @@ int main() { | |||
| 		digits[digit_num] = number & lsb_bitmask; | ||||
| 		 | ||||
| 		// Shift off the bits we have put in to the array
 | ||||
| 		number = ( number >> bits_in_char ); | ||||
| 		number >>= bits_in_char; | ||||
| 
 | ||||
| 		++digit_num; | ||||
| 	} | ||||
|  | @ -91,14 +91,7 @@ int main() { | |||
| 	// Loop the other way so the first digit we get out is MSB
 | ||||
| 	while ( digit_num >= 0 )  | ||||
| 	{ | ||||
| 		if ( digits[digit_num] < 10 ) | ||||
| 		{ | ||||
| 			std::cout << digits[digit_num]; | ||||
| 		} | ||||
| 		else  | ||||
| 		{ | ||||
| 			std::cout << b32_lookup_table[digits[digit_num] - 10]; | ||||
| 		} | ||||
| 		std::cout << b32_lookup_table[digits[digit_num]]; | ||||
| 
 | ||||
| 		digit_num--; | ||||
| 	} | ||||
|  |  | |||
		Reference in a new issue