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
86
87
88
89
90
91
92
93
94
95
96
97
|
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
// Function declarations
void byteArrayToHexString_point(const unsigned char* byteArray, size_t length, char* hexString);
void byteArrayToHexString_snprintf(const unsigned char* byteArray, size_t length, char* hexString);
void byteArrayToHexString_idx(const unsigned char* byteArray, size_t length, char* hexString);
int main() {
// Example large byte array
size_t length = 256; // 1 million bytes
unsigned char *byteArray = (unsigned char*)malloc(length * sizeof(unsigned char));
for (size_t i = 0; i < length; i++) {
byteArray[i] = i % 256; // Filling with example data
}
// Allocate memory for the hex string (2 characters per byte + 1 for the null terminator)
char* hexString = (char*)malloc((length * 2 + 1) * sizeof(char));
if (hexString == NULL || byteArray == NULL) {
fprintf(stderr, "Memory allocation failed\n");
return EXIT_FAILURE;
}
int loop_times=1000;
// Measure the time taken for the conversion
clock_t start = clock();
for(int i=0; i<loop_times; i++)
{
byteArrayToHexString_point(byteArray, length, hexString);
}
clock_t end = clock();
// Print the time taken
double time_spent = (double)(end - start) / CLOCKS_PER_SEC;
printf("byteArrayToHexString_point Time taken: %f seconds\n", time_spent/loop_times);
start = clock();
for(int i=0; i<loop_times; i++)
{
byteArrayToHexString_snprintf(byteArray, length, hexString);
}
end = clock();
// Print the time taken
time_spent = (double)(end - start) / CLOCKS_PER_SEC;
printf("byteArrayToHexString_snprintf: Time taken: %f seconds\n", time_spent/loop_times);
start = clock();
for(int i=0; i<loop_times; i++)
{
byteArrayToHexString_idx(byteArray, length, hexString);
}
end = clock();
// Print the time taken
time_spent = (double)(end - start) / CLOCKS_PER_SEC;
printf("byteArrayToHexString_idx Time taken: %f seconds\n", time_spent/loop_times);
// Free the allocated memory
free(hexString);
free(byteArray);
return 0;
}
// Function to convert byte array to hex string
void byteArrayToHexString_point(const unsigned char* byteArray, size_t length, char* hexString) {
const char hexChars[] = "0123456789abcdef";
char* ptr = hexString;
for (size_t i = 0; i < length; i++) {
*ptr++ = hexChars[(byteArray[i] >> 4) & 0x0F];
*ptr++ = hexChars[byteArray[i] & 0x0F];
}
*ptr = '\0'; // Null-terminate the string
}
// Function to convert byte array to hex string
void byteArrayToHexString_snprintf(const unsigned char* byteArray, size_t length, char* hexString) {
for (size_t i = 0; i < length; i++) {
sprintf(hexString + (i * 2), "%02x", byteArray[i]);
}
hexString[length * 2] = '\0'; // Null-terminate the string
}
void byteArrayToHexString_idx(const unsigned char* byteArray, size_t length, char* hexString) {
const char hexChars[] = "0123456789abcdef";
for (size_t i = 0; i < length; i++) {
hexString[i * 2] = hexChars[(byteArray[i] >> 4) & 0x0F];
hexString[i * 2 + 1] = hexChars[byteArray[i] & 0x0F];
}
hexString[length * 2] = '\0'; // Null-terminate the string
}
|