This commit is contained in:
Nils K
2020-04-19 23:32:17 +02:00
parent f036761313
commit 429efe3f49
4 changed files with 59 additions and 10 deletions

View File

@@ -1,2 +1,2 @@
build: branch_predict.c
gcc branch_predict.c -O2 -o branch_predict.exe
gcc branch_predict.c -O2 -lssl -lcrypto -o branch_predict.exe

Binary file not shown.

View File

@@ -3,6 +3,11 @@
#include<string.h>
#include<time.h>
//////////
#include <openssl/sha.h>
#include <openssl/bio.h>
/////////
/*
* range of the chars in the randomly generated string
*
@@ -11,8 +16,13 @@
*
*/
#define CHAR_START 33
#define CHAR_END 127
#define CHAR_START '1'
#define CHAR_END '9'
//#define MEASURE_FUNC string_bit_count
//#define MEASURE_FUNC string_bit_count2
//#define MEASURE_FUNC string_bit_count3
#define MEASURE_FUNC sha1_int_from_string
/*
* returns dynamically allocated random string
@@ -54,6 +64,9 @@ char** read_strings(const char* fileName, size_t* nRead);
* count set bits in string s
*/
int string_bit_count(char* s);
int string_bit_count2(const char* s);
int string_bit_count3(const char* s);
int sha1_int_from_string(const char* s);
/*
* returns malloced memory of size memSize
@@ -66,6 +79,9 @@ int string_sort_func(const void* a, const void* b);
void usage_and_exit();
#define PRE_STR_HELPER(token) #token
#define PRE_STR(token) PRE_STR_HELPER(token)
void usage_and_exit() {
printf("usage\n");
printf("<prog> measure <filename>\n");
@@ -96,10 +112,11 @@ int main(int argc, char* argv[])
unsigned long long counter = 0;
clock_t t1, t0 = clock();
for(; *p; ++p) {
counter += string_bit_count(*p);
counter += MEASURE_FUNC(*p);
}
t1 = clock();
printf("measure file: <%s>\n", argv[2]);
printf("measure function: <%s>\n", PRE_STR(MEASURE_FUNC));
printf("counter = %llu\n", counter);
printf("time = %lf ms\n", (t1 - t0) / (double) CLOCKS_PER_SEC * 1000.0);
@@ -187,6 +204,25 @@ int string_bit_count(char* s) {
//printf("count = %d\n", count);
return count;
}
int string_bit_count3(const char* s) {
static const unsigned char lookup[] ={0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 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, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8};
int count = 0;
const char* p = s;
for(; *p; ++p) {
count += lookup[*p];
}
return count;
}
int string_sort_func(const void* a, const void* b) {
char* sa = *((char**) a);
char* sb = *((char**) b);
@@ -225,3 +261,16 @@ char** read_strings(const char* fileName, size_t* nRead) {
return out_array;
}
int sha1_int_from_string(const char* s) {
SHA_CTX c;
SHA1_Init(&c);
char sha1_bytes[20];
SHA1_Update(&c, s, strlen(s));
SHA1_Final(sha1_bytes, &c);
int count = 0;
for(int i=0; i<20; ++i) {
count += sha1_bytes[i];
}
return count;
}

View File

@@ -1,7 +1,7 @@
./branch_predict generate 5000000 10 10 > unsorted_sample01.txt
./branch_predict sort unsorted_sample01.txt > issorted_sample01.txt
echo "head of unsorted_sample01.txt" >> output.txt
./branch_predict.exe generate 5000000 4 4 > unsorted_sample01.txt
./branch_predict.exe sort unsorted_sample01.txt > issorted_sample01.txt
echo -e "\nhead of unsorted_sample01.txt" >> output.txt
head unsorted_sample01.txt >> output.txt
echo "\n" >> output.txt
./branch_predict measure unsorted_sample01.txt >> output.txt
./branch_predict measure issorted_sample01.txt >> output.txt
echo -e "\n" >> output.txt
./branch_predict.exe measure unsorted_sample01.txt >> output.txt
./branch_predict.exe measure issorted_sample01.txt >> output.txt