I’m currently working on generating a galaxy, which is random every time. My problem now is that i need to save the position data generated some how. I’ve tried making a method that writes every star into an XML, the only problem is that it takes maybe 0.5 sec for it to write each line. And i’m currently testing with only 7500 stars.
Basically what it writes is:
//x, y and z are float values
<system position= (x, y, z) />
for every object created.
Since this is just the beginning I will eventually need to store more data in each system, which means it will take even longer to write.
So now i’m looking for a better way to write XML or even a completely other way to store the data.
I’m not an expert on XML, but it shouldn’t be taking anywhere near as long as that.
Can you show us more code?
I created a small test:
using UnityEngine;
public class CallXML : MonoBehaviour
{
public int items = 1000;
void Start()
{
float start = Time.realtimeSinceStartup;
XML xml = new XML();
xml.WriteTest(items);
print("Completed Test in " + (Time.realtimeSinceStartup - start) + "s with " + items + " items");
}
}
Which calls:
using System.Xml;
using System;
class XML
{
Random random = new Random();
public void WriteTest(int items)
{
using (XmlWriter writer = XmlWriter.Create("Universe.xml"))
{
writer.WriteStartDocument();
writer.WriteStartElement("Milky Way");
for (int i = 0; i< items ; i++)
{
writer.WriteStartElement("System");
writer.WriteElementString("x", random.Next(0, 100).ToString());
writer.WriteElementString("y", random.Next(0, 100).ToString());
writer.WriteElementString("z", random.Next(0, 100).ToString());
writer.WriteEndElement();
}
writer.WriteEndElement();
writer.WriteEndDocument();
}
}
}
I saved 1Mn items (4Mn+ elements) in 6 seconds.
Thanks, but i figured it out, i had accidentally but my doc.Save(file) inside the loop which made it save each time, which slowed it down. Now it takes about 0.15 seconds (154ms) to write 7500 lines.