Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduced String creation during 'CSV' output. #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 11 additions & 27 deletions WeatherSystem/Assets/WeatherLogger.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEngine;
using WeatherSystem;

Expand Down Expand Up @@ -28,8 +29,8 @@ private void Awake()
weatherManager = FindObjectOfType<WeatherManager>();
}

string[] headings = new string[] { "time", "weatherType", "humEnum", "hum", "tempEnum", "temp", "intensity", "windInstance", "windTracked" };
AddToLog(ToCSV(headings));
string[] headings = { "time", "weatherType", "humEnum", "hum", "tempEnum", "temp", "intensity", "windInstance", "windTracked" };
AddToLog(string.Format("{0}{1}", string.Join(",", headings), System.Environment.NewLine));
}

private void Start()
Expand All @@ -44,12 +45,10 @@ private void Update()
{
timeSinceLog += Time.deltaTime;

if (timeSinceLog >= logTime)
{
LogWeatherData();

timeSinceLog = timeSinceLog - logTime; //carry over the remainder time (min = 0f)
}
if (timeSinceLog < logTime) return;

LogWeatherData();
timeSinceLog = timeSinceLog - logTime; //carry over the remainder time (min = 0f)
}

private void LogWeatherData()
Expand All @@ -76,28 +75,13 @@ private void LogWeatherData()
HumidityVariables humEnum = hum.ToHumidityValue();

float time = Time.timeSinceLevelLoad;
AddToLog(ToCSV(new object[] { time, weatherType, humEnum, hum, tempEnum, temp, intensity, windInstance, windTracked }));
}

private string ToCSV(object[] data)
{
string text = "";

for(int i = 0; i < data.Length; i++)
{
text += data[i].ToString();

if(i != data.Length - 1)
{
text += ",";
}
}

return text + System.Environment.NewLine;
var data = new object[] {time, weatherType, humEnum, hum, tempEnum, temp, intensity, windInstance, windTracked};
AddToLog(string.Format("{0}{1}", string.Join(",", (from value in data select value.ToString()).ToArray()),
System.Environment.NewLine));
}

private void AddToLog(string text)
{
File.AppendAllText(loggingFolder + "/log.txt", text);
File.AppendAllText(string.Format("{0}/log.txt", loggingFolder), text);
}
}