Created
October 19, 2013 23:48
-
-
Save habibutsu/7062991 to your computer and use it in GitHub Desktop.
Different ways to copy file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <cstdio> // fopen, fclose, fread, fwrite, BUFSIZ | |
#include <ctime> | |
using namespace std; | |
int main() { | |
clock_t start, end; | |
start = clock(); | |
// BUFSIZE default is 8192 bytes | |
// BUFSIZE of 1 means one chareter at time | |
// good values should fit to blocksize, like 1024 or 4096 | |
// higher values reduce number of system calls | |
// size_t BUFFER_SIZE = 4096; | |
char buf[BUFSIZ]; | |
size_t size; | |
FILE* source = fopen("from.ogv", "rb"); | |
FILE* dest = fopen("to.ogv", "wb"); | |
// clean and more secure | |
// feof(FILE* stream) returns non-zero if the end of file indicator for stream is set | |
while (size = fread(buf, 1, BUFSIZ, source)) { | |
fwrite(buf, 1, size, dest); | |
} | |
fclose(source); | |
fclose(dest); | |
end = clock(); | |
cout << "CLOCKS_PER_SEC " << CLOCKS_PER_SEC << "\n"; | |
cout << "CPU-TIME START " << start << "\n"; | |
cout << "CPU-TIME END " << end << "\n"; | |
cout << "CPU-TIME END - START " << end - start << "\n"; | |
cout << "TIME(SEC) " << static_cast<double>(end - start) / CLOCKS_PER_SEC << "\n"; | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <fstream> | |
#include <ctime> | |
#include <algorithm> | |
#include <iterator> | |
using namespace std; | |
int main() { | |
clock_t start, end; | |
start = clock(); | |
ifstream source("from.ogv", ios::binary); | |
ofstream dest("to.ogv", ios::binary); | |
istreambuf_iterator<char> begin_source(source); | |
istreambuf_iterator<char> end_source; | |
ostreambuf_iterator<char> begin_dest(dest); | |
copy(begin_source, end_source, begin_dest); | |
source.close(); | |
dest.close(); | |
end = clock(); | |
cout << "CLOCKS_PER_SEC " << CLOCKS_PER_SEC << "\n"; | |
cout << "CPU-TIME START " << start << "\n"; | |
cout << "CPU-TIME END " << end << "\n"; | |
cout << "CPU-TIME END - START " << end - start << "\n"; | |
cout << "TIME(SEC) " << static_cast<double>(end - start) / CLOCKS_PER_SEC << "\n"; | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <sys/sendfile.h> // sendfile | |
#include <fcntl.h> // open | |
#include <unistd.h> // close | |
#include <sys/stat.h> // fstat | |
#include <sys/types.h> // fstat | |
#include <ctime> | |
using namespace std; | |
int main() { | |
clock_t start, end; | |
start = clock(); | |
int source = open("from.ogv", O_RDONLY, 0); | |
int dest = open("to.ogv", O_WRONLY | O_CREAT /*| O_TRUNC/**/, 0644); | |
// struct required, rationale: function stat() exists also | |
struct stat stat_source; | |
fstat(source, &stat_source); | |
sendfile(dest, source, 0, stat_source.st_size); | |
close(source); | |
close(dest); | |
end = clock(); | |
cout << "CLOCKS_PER_SEC " << CLOCKS_PER_SEC << "\n"; | |
cout << "CPU-TIME START " << start << "\n"; | |
cout << "CPU-TIME END " << end << "\n"; | |
cout << "CPU-TIME END - START " << end - start << "\n"; | |
cout << "TIME(SEC) " << static_cast<double>(end - start) / CLOCKS_PER_SEC << "\n"; | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <fstream> | |
#include <ctime> | |
using namespace std; | |
int main() { | |
clock_t start, end; | |
start = clock(); | |
ifstream source("from.ogv", ios::binary); | |
ofstream dest("to.ogv", ios::binary); | |
// file size | |
source.seekg(0, ios::end); | |
ifstream::pos_type size = source.tellg(); | |
source.seekg(0); | |
// allocate memory for buffer | |
char* buffer = new char[size]; | |
// copy file | |
source.read(buffer, size); | |
dest.write(buffer, size); | |
// clean up | |
delete[] buffer; | |
source.close(); | |
dest.close(); | |
end = clock(); | |
cout << "CLOCKS_PER_SEC " << CLOCKS_PER_SEC << "\n"; | |
cout << "CPU-TIME START " << start << "\n"; | |
cout << "CPU-TIME END " << end << "\n"; | |
cout << "CPU-TIME END - START " << end - start << "\n"; | |
cout << "TIME(SEC) " << static_cast<double>(end - start) / CLOCKS_PER_SEC << "\n"; | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <fcntl.h> // open | |
#include <unistd.h> // read, write, close | |
#include <cstdio> // BUFSIZ | |
#include <ctime> | |
using namespace std; | |
int main() { | |
clock_t start, end; | |
start = clock(); | |
// BUFSIZE defaults to 8192 | |
// BUFSIZE of 1 means one chareter at time | |
// good values should fit to blocksize, like 1024 or 4096 | |
// higher values reduce number of system calls | |
// size_t BUFFER_SIZE = 4096; | |
char buf[BUFSIZ]; | |
size_t size; | |
int source = open("from.ogv", O_RDONLY, 0); | |
int dest = open("to.ogv", O_WRONLY | O_CREAT /*| O_TRUNC/**/, 0644); | |
while ((size = read(source, buf, BUFSIZ)) > 0) { | |
write(dest, buf, size); | |
} | |
close(source); | |
close(dest); | |
end = clock(); | |
cout << "CLOCKS_PER_SEC " << CLOCKS_PER_SEC << "\n"; | |
cout << "CPU-TIME START " << start << "\n"; | |
cout << "CPU-TIME END " << end << "\n"; | |
cout << "CPU-TIME END - START " << end - start << "\n"; | |
cout << "TIME(SEC) " << static_cast<double>(end - start) / CLOCKS_PER_SEC << "\n"; | |
return 0; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <fstream> | |
#include <ctime> | |
using namespace std; | |
int main() { | |
clock_t start, end; | |
start = clock(); | |
ifstream source("from.ogv", ios::binary); | |
ofstream dest("to.ogv", ios::binary); | |
dest << source.rdbuf(); | |
source.close(); | |
dest.close(); | |
end = clock(); | |
cout << "CLOCKS_PER_SEC " << CLOCKS_PER_SEC << "\n"; | |
cout << "CPU-TIME START " << start << "\n"; | |
cout << "CPU-TIME END " << end << "\n"; | |
cout << "CPU-TIME END - START " << end - start << "\n"; | |
cout << "TIME(SEC) " << static_cast<double>(end - start) / CLOCKS_PER_SEC << "\n"; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment