98 lines
3.2 KiB
C++
98 lines
3.2 KiB
C++
#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
|
|
}
|