changeset 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 5bcf04234f80 30dade7bc2bc 94751ad27fd6
files core/WriteFile.cpp include/WriteFile.h
diffstat 2 files changed, 14 insertions(+), 11 deletions(-) [+]
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;
 }
--- a/include/WriteFile.h	Wed Oct 14 17:55:58 2015 +0100
+++ b/include/WriteFile.h	Sun Oct 18 01:13:40 2015 +0100
@@ -59,7 +59,7 @@
     static bool staticConstructed;
 	static void staticConstructor();
 	static std::vector<WriteFile *> objAddrs;
-	void writeOutput(bool writeAll);
+	void writeOutput(bool flush);
 public:
 	WriteFile();
 	/**
@@ -111,7 +111,7 @@
     static int getNumInstances();
     static void writeAllHeaders();
     static void writeAllFooters();
-    static void writeAllOutputs();
+    static void writeAllOutputs(bool flush);
     static void startThread();
     static void stopThread();
     static void run();