diff core/WriteFile.cpp @ 159:1e7db6610600

Increased buffer size, improves performances (but increases memory usage) for binary mode
author Giulio Moro <giuliomoro@yahoo.it>
date Sun, 18 Oct 2015 01:13:40 +0100
parents 45fc760622c9
children 9bfe04d184fb e4392164b458
line wrap: on
line diff
--- a/core/WriteFile.cpp	Wed Oct 14 17:55:58 2015 +0100
+++ b/core/WriteFile.cpp	Sun Oct 18 01:13:40 2015 +0100
@@ -35,7 +35,7 @@
 	file = fopen(filename, "w");
 	variableOpen = false;
 	lineLength = 0;
-	echo = false;
+	setEcho(false);
 	bufferLength = 0;
 	textReadPointer = 0;
 	binaryReadPointer = 0;
@@ -53,6 +53,8 @@
 
 void WriteFile::setFileType(WriteFileType newFileType){
 	fileType = newFileType;
+	if(fileType == kBinary)
+		setLineLength(1);
 }
 void WriteFile::setEcho(bool newEcho){
 	echo=newEcho;
@@ -85,7 +87,7 @@
 								formatTokens[n], buffer[textReadPointer]);
 			stringBufferPointer += numOfCharsWritten;
 			textReadPointer++;
-			if (textReadPointer >= bufferLength){
+			if(textReadPointer >= bufferLength){
 				textReadPointer -= bufferLength;
 			}
 		}
@@ -96,7 +98,7 @@
 void WriteFile::setLineLength(int newLineLength){
 	lineLength=newLineLength;
 	free(buffer);
-	bufferLength = lineLength * 10000; // circular buffer of length 10000 lineLenghts
+	bufferLength = lineLength * (int)1e7; // circular buffer of length 1e7 lineLenghts
 	buffer = (float*)malloc(sizeof(float) * bufferLength);
 }
 
@@ -202,13 +204,13 @@
 	}
 }
 
-void WriteFile::writeOutput(bool writeAll){
+void WriteFile::writeOutput(bool flush){
 	while((echo == true || fileType == kText) && getOffsetFromPointer(textReadPointer) >= lineLength){ //if there is less than one line worth of data to write, skip over.
 							 	 // So we make sure we only write full lines
 		writeLine();
 	}
 	if(fileType == kBinary){
-		int numBinaryElementsToWriteAtOnce = 100;
+		int numBinaryElementsToWriteAtOnce = 3*(int)1e5;
 		while(getOffsetFromPointer(binaryReadPointer) > numBinaryElementsToWriteAtOnce){
 			int elementsToEndOfBuffer = bufferLength - binaryReadPointer;
 			int numberElementsToWrite = numBinaryElementsToWriteAtOnce < elementsToEndOfBuffer ?
@@ -219,7 +221,7 @@
 				binaryReadPointer = 0;
 			}
 		}
-		if(writeAll == true){ // flush all the buffer to the file
+		if(flush == true){ // flush all the buffer to the file
 			while(getOffsetFromPointer(binaryReadPointer) != 0){
 				binaryReadPointer += fwrite(&(buffer[binaryReadPointer]), sizeof(float), 1, file);
 				if(binaryReadPointer >= bufferLength){
@@ -230,9 +232,9 @@
 	}
 }
 
-void WriteFile::writeAllOutputs(){
+void WriteFile::writeAllOutputs(bool flush){
 	for(unsigned int n = 0; n < objAddrs.size(); n++){
-		objAddrs[n] -> writeOutput(false);
+		objAddrs[n] -> writeOutput(flush);
 	}
 }
 
@@ -279,9 +281,10 @@
 	threadRunning = true;
 	writeAllHeaders();
 	while(threadShouldExit()==false){
-		writeAllOutputs();
+		writeAllOutputs(false);
 		usleep(sleepTimeMs*1000);
 	}
+	writeAllOutputs(true);
 	writeAllFooters(); // when ctrl-c is pressed, the last line is closed and the file is closed
 	threadRunning = false;
 }