Hello, I'm currently trying to write my own hex editor, however, I've noticed that my program is horribly slow at loading files. Anything in the range of 500kb is fine, but afterwards in the 1MB range, the delay to the completion of the whole operation is noticable in seconds, e.g a 4.5 MB file takes about 12 seconds to process on a good run. I know this is a problem, because I've used other hex editors that will load a 200+MB file in less than that time.
I'm pretty sure this is due to the fact that my program reads the file byte by byte and performs an alogrithm on each byte to convert them into hexadecimal format. Currently, I'm reading in one unsigned char per byte in the file, them calling my function to convert that into a string object. I've tried reducing the number of reads, such as in reading 16 bytes at a time, but that still won't reduce the number of times I need to call the function to convert it to hex, and thus far, has not been able to produce any noticable gains in speed.
I've cut out all of the GUI lines of code and altered the following code to work as a basic command line representation of only reading in the file, which is where I'm having bottleneck problems. I've been trying to optimize this section for days, but I can't figure out how it could be done. I'm sure there's something efficient that I should be doing, but I'm horribly unaware of it.
Sorry about the lack of indentation, I don't usually use it. umulti_base is the function that manually converts an integer into it's corresponding hex value as a string. The first parameter is the actual integer to be converted and the second integer is the conversion base. Any suggestions on how this can be more efficient?Code:#include <iostream> #include <fstream> using namespace std; /* Convert Int To Hex String */ string umulti_base(int input1, int input2) { /* Temporary Function Variable */ string ans = "", bit = "0123456789ABCDEF"; int incr = 1, value = input1, range = input2, count = 0; /* Grab Highest Power On Base */ while((incr * range) <= value) { incr *= range; ++count; } while(count >= 0) { for(int x = range; --x >= 0;) { if((value - (incr * x)) >= 0) { ans += bit[x]; value -= (incr * x); break; } } incr /= range; --count; } return ans; } int main() { /* Variables */ string file_name; unsigned char mem; ifstream file; int file_size = 0, b = 0, e = 0, file_get = 0; /* Get File Name Input */ cout<<"Enter File Name : "; cin>>file_name; cin.ignore(); /* Open File */ file.open(file_name.c_str(), ios::binary); /* If File Can Be Opened */ if(file.is_open()) { /* Get File Size & Reset File Pointer */ file.seekg(0, ios::beg); b = file.tellg(); file.seekg(0, ios::end); e = file.tellg(); file_size = e - b; file.clear(); file.seekg(0, ios::beg); /* Cycle Through File Byte By Byte & Convert To Hexadecimal */ for(int x = -1; ++x < file_size;) { /* Read 1 Byte Into Char */ file.read((char*)(&mem), 1); /* Convert to Hex */ cout<< umulti_base((int)mem, 16) << "\t"; } cout<< "\nTask Complete \n"; } /* If File Cannot Be Opened */ else { cout<<"File Could Not Be Opened \n"; } return 0; }