-
Notifications
You must be signed in to change notification settings - Fork 0
/
entropy.c
85 lines (81 loc) · 1.75 KB
/
entropy.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include "entropy.h"
#include <math.h>
#include <malloc.h>
#include <string.h>
#include <sys/time.h>
float calculate_entropy_direct(uint8_t* codes,int len)
{
int bins[26];
for(int i=0;i!=26;i++)
{
bins[i] = 0;
}
for(int i=0;i!=len;i++)
{
bins[codes[i]]++;
}
float probility = 1.0f;
float entropy = 0.0f;
for(int i=0;i!=26;i++)
{
if(bins[i]!=0)
{
probility = (float)bins[i] / (float)len;
entropy += -probility*logf(probility);
}
}
return entropy;
}
uint8_t* text_to_codes(char* text,size_t* len)
{
size_t text_len = strlen(text);
uint8_t* codes = (uint8_t*)malloc(text_len);
if(codes == NULL)
{
*len = 0;
return NULL;
}
for(int i=0;i!=text_len;i++)
{
codes[i] = text[i] - 'A';
if(codes[i]<0 || codes[i]>=26)
{
free(codes);
*len = 0;
return NULL;
}
}
*len = text_len;
return codes;
}
float calculate_entropy(char* text)
{
size_t len = 0;
uint8_t* codes = text_to_codes(text,&len);
if(codes == NULL)
{
return -1.0f;
}
float entropy = calculate_entropy_direct(codes,len);
free(codes);
return entropy;
}
double entropy_speed_test(char* src,int iteration)
{
size_t len;
uint8_t* codes = text_to_codes(src,&len);
if(codes == NULL)
{
return 0.0;
}
struct timeval start,end;
gettimeofday(&start,NULL);
for(int i=0;i!=iteration;i++)
{
calculate_entropy_direct(codes,len);
}
gettimeofday(&end,NULL);
free(codes);
double speed = ((double)iteration*1e6)/(double)(end.tv_sec*1000000+end.tv_usec-(start.tv_sec*1000000+start.tv_usec));
return speed;
}