I am currently working with a sample P2P program to share any type of files with others. A file is now divided into 5 parts and save 5 parts into harddisk before starting p2p sharing. However, I found that this algorithm is too dummy and i would like to modify it with 5 pointers pointing to the memory instead of writing 5 parts into the harddisk to save memory space and increase the program speed. However, I got no idea of using the pointers.
Here's the code I am currently using. It works but too dummy.
int main(int argc, char* argv[])
{
char * memblock;
if (Readfile.is_open())
{
size = Readfile.tellg();
memblock = new char [size];
int ss = size;
cout<<"FileSize:"<<ss<<'\n';
Readfile.seekg (0, ios::beg);
// I use cout <<&memblock <<'\n'; to check the location of the memory and I found that each time the memory location is the same. Also I use cout <<memblock <<'\n'; to the check the content in the memory and the content varies each time
What if the file is 4 bytes long? What if it is 10 GiB long? Will you load it all to memory?
You should divide the file into blocks of constant size (e.g. 4K) and then load what you need.
helios, thank you for your answering.
yes, I tried a 500 mb video file in my program and the speed is extremely slow.
It took a long time to load the 500mb file into memory and start divide and copy the data into 5 parts respectively.
I am trying to divide the 500mb file into 50 parts, i.e. each part is 10mb. Can you provide some approaches to me that make the program run faster?
It seems that using Read and Write method in I/O stream is slow and not feasible.
I see you're using an std::ofstream to read data. How that program is even compiling is beyond me, but ofstream is for writing data (output and input is always from and into memory).
read() and write() are the fastest methods for said operations. You either a) don't have enough physical memory, so the OS need to use virtual memory, which very slow, or b) your disk or your bus are too slow. I myself can load 500 MB in around 30 secs.
Don't open and close the same file over and over. Instead, open it once, operate, position the file pointer, operate again, etc., then close it.
helios, thank you
however, can you further explain how to position the file pointer ? or can you provide some sample code to me, i have no idea. Thank you