Skip to content

Instantly share code, notes, and snippets.

@dejaime
Last active January 3, 2016 10:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dejaime/8450606 to your computer and use it in GitHub Desktop.
Save dejaime/8450606 to your computer and use it in GitHub Desktop.
Comparison between direct binary data copy and the read, interpret, set method.
#include <iostream>
#include <unistd.h>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <time.h>
#define MAPW 7500
#define MAPH 7500
#define TEXT_FILE_NAME "mapAsText.map"
#define BINARY_FILE_NAME "mapAsBinary.map"
class Tile {
unsigned int tileType;
float friction;
public:
void set_tileType (unsigned int p_type) { tileType = p_type; }
void set_friction (float p_friction) { friction = p_friction; }
int get_tileType () const { return tileType; }
float get_friction () const { return friction; }
};
class Map {
unsigned int width, height;
Tile *tileMatrix; //This is the pointer we'll use for our tile array.
unsigned int nextTile; //Starting from 0, we will increment this until
// all tiles are created.
public:
Map (unsigned int p_mapWidth, unsigned int p_mapHeight) {
//Allocate memory for all the necessary tiles.
tileMatrix = (Tile*)std::malloc( p_mapWidth*p_mapHeight * sizeof(Tile));
nextTile = 0;
width = p_mapWidth;
height = p_mapHeight;
}
bool is_ready () {
//Checks if all tiles have been created successfully.
return (nextTile == width * height) ? true : false;
/* same as:
* if (nextTile == width * height) return true;
* else return false;
* */
}
bool add_tile (unsigned int p_tileType, float p_friction) {
//if (is_ready()) return true; //Error, all tiles already created.
tileMatrix[nextTile].set_tileType(p_tileType);
tileMatrix[nextTile].set_friction(p_friction);
++nextTile;
return false;
}
bool print_map () {
std::cout<<"\n\nPrinting Map\n";
for (unsigned int i = 0; i < height*width; ++i) {
if (i % width == 0) std::cout<<"\n";
std::cout <<
" ["<< tileMatrix[ i ].get_tileType() <<
", "<< tileMatrix[ i ].get_friction() << "]";
}
std::cout<<"\n\n";
return false;
}
void flush () {
nextTile = 0; //If we need to "re-create" the map.
}
//Text save and load
void save_as_text () {
FILE* f = fopen (TEXT_FILE_NAME, "w");
for (unsigned int i = 0; i < height*width; ++i) {
fprintf (f, "%d %f ", tileMatrix[ i ].get_tileType(), tileMatrix[ i ].get_friction() );
}
fclose (f);
}
void load_text () {
FILE* f;
unsigned int tempType;
float tempFriction;
f = fopen (TEXT_FILE_NAME, "r");
if (f == NULL) return;
flush (); //So we can reload the map
for (unsigned int i = 0; i < height*width; ++i) {
fscanf (f, "%d %f ", &tempType, &tempFriction );
add_tile(tempType, tempFriction);
}
fclose (f);
}
//Binary save and load.
void save_as_binary () {
//Open the file for binary writing (wb)
FILE* f = fopen (BINARY_FILE_NAME, "wb");
//Copy the entire chunk of data in one function call
fwrite (tileMatrix, sizeof(Tile), height*width, f);
fclose (f);
}
void load_binary () {
FILE* f = fopen (BINARY_FILE_NAME, "rb");
if (f == NULL) return;
fread (tileMatrix, sizeof(Tile), height*width, f);
fclose (f);
}
};
int main (){
//Variables to mark the time consumed by each function.
clock_t totalTime, manualCreation, loadText, loadBinary, saveText, saveBinary;
totalTime = clock();
std::cout << "Starting test for a ["<< MAPW << ", " << MAPH <<"] wide set." << std::endl;
std::cout << "Searching for a big enough memory block (" << MAPW * MAPH * sizeof(Tile) /(1000*1000) <<" mb )"<< std::endl;
Map map( MAPW, MAPH );
std::cout << "Creating the map manually." << std::endl;
//Create the map manually.
manualCreation = clock();
for (int i = 0; i < MAPW*MAPH ; ++i) //See MAPW and MAPH on the top ^
map.add_tile (i, i+10); //Add a single tile with 'random' values.
manualCreation = clock() - manualCreation;
std::cout << "Saving the map to the files." << std::endl;
//Save the map as text.
saveText = clock();
map.save_as_text();
saveText = clock() - saveText;
//Save it as binary.
saveBinary = clock();
map.save_as_binary();
saveBinary = clock() - saveBinary;
std::cout << "Loading the map from the files." << std::endl;
//Load it from the text file.
loadText = clock();
map.load_text();
loadText = clock() - loadText;
//Load it from the binary file.
loadBinary = clock();
map.load_binary();
loadBinary = clock() - loadBinary;
totalTime = clock() - totalTime;
//Print it to the console (for tests with small maps, of course).
//map.print_map();
std::cout<<"\n\nTotal Time for each of the tasks - "<<
"\n\tManual Creation:\t" << ((float)manualCreation)/CLOCKS_PER_SEC << " s" <<
"\n\tLoad from TEXT file:\t" << ((float)loadText)/CLOCKS_PER_SEC << " s" <<
"\n\tLoad from BINARY file:\t" << ((float)loadBinary)/CLOCKS_PER_SEC << " s" <<
"\n\tSave to TEXT file:\t" << ((float)saveText)/CLOCKS_PER_SEC << " s" <<
"\n\tSave to BINARY file:\t" << ((float)saveBinary)/CLOCKS_PER_SEC << " s" <<
"\nTotal Time:\t" << ((float)totalTime)/CLOCKS_PER_SEC << " s\n" << std::endl;
std::cout<<"Deleting Files.\n";
int err = remove(TEXT_FILE_NAME);
if (err) std::cout<<"\nThere was a problem deleting the Text File, please do so manually. Error: " << err;
err = remove(BINARY_FILE_NAME);
if (err) std::cout<<"\nThere was a problem deleting the Binary File, please do so manually. Error: " << err;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment