programming a for loop inside another makes my program be VERY laggy. is there a reason for this?
program still works though
using JavaScript
programming a for loop inside another makes my program be VERY laggy. is there a reason for this?
program still works though
using JavaScript
Okay, this is a little hard to understand because it’s German, but I think I get it. First of all, why would you have two for loops counting to a hundred? Do it all in the first. Second of all, the loop is printing something a hundred times, which might be the biggest cause of the game running slow. I know that at least Debug.Log a few hundred times/sec will slow things down a lot. Third of all, do all hundred things have to be set every frame? That’s dozens of times a second. You could make things quicker if you set the first 25 in one frame, the second 25 in the next, etc and then again.
Joshua is right. There’s no need for two loops. Debug.Log is really slow, i guess if you remove it you won’t even notice this piece of code. Another thing: You split your whole csv-file EVERY iteration. Do it once outside the loop. Furthermore “magic”-numbers like 100 are bad. If the file contains less lines you will get an out-of-bounds error. Just use the Length of your line-array.
function lese_datei() // Öffnen und Parsen der Datei.
{
Zeilen = CSVDatei.text.Split("
“[0]); // Parsen an Zeilenendekennung
for(i=1;i < Zeilen.Length; i++)
{
Werte = Zeilen*.Split(”;"[0]); // Parsen am Semikolon*
var Geschwindigkeit_rotation = Werte[4];
var Umdrehungen_rotation = Werte[9];
var Motorhitze_rotation = Werte[8];
print(Geschwindigkeit_rotation + "
" + Umdrehungen_rotation + "
" + Motorhitze_rotation);
}
}
There is a very high chance that the body of the for loop is coded with a high cost task.
Also please read the comments place below your question. I would suggest clarifying your question so a proper answer can be given.
And finally, how did you see that is was high cost? External profiler?
Sorry for using answer function, cant comment due to RMD’s (Random Moronic Downvotes)
Joshua, i’ve optimized my script a bit now, but how do i “set the first 25 in one frame, the second 25 in the next”?
here’s the new code
function lese_datei() // Öffnen und Parsen der Datei.
{
for(i=4;i < 104; i++)
{
Zeilen = CSVDatei.text.Split("
“[0]); // Parsen an Zeilenendekennung
Werte = Zeilen*.Split(”;"[0]); // Parsen am Semikolon*
var Geschwindigkeit_pos = int.Parse(Werte[4]);*
var Umdrehungen_pos = int.Parse(Werte[8]);*
var Motorhitze_pos = int.Parse(Werte[9]);*
Geschwindigkeit_rotation = (Geschwindigkeit_pos / 300.0) * 180.0;
Umdrehungen_rotation = (Umdrehungen_pos / 8000.0) * 240.0;
Motorhitze_rotation = Motorhitze_pos;*
*print(“Geschwindigkeit:”+Geschwindigkeit_pos+"
“+“RPM:”+Umdrehungen_pos+”
“+“Temperatur:”+Motorhitze_pos+”
“+“Gradzahl speed:”+Geschwindigkeit_rotation+”
“+“Gradzahl rpm:”+Umdrehungen_rotation+”
"+“Gradzahl Temperatur:”+ Motorhitze_rotation);*
}*
}*
okay, this made my code A LOT faster, thank you very much guys. That’s upvotes for you.
Well NOW i have got another issue.
It fetches the data like superfast, i only want ONE reading per second…
is it possible to do this somehow, maybe by using time.deltaTime??
//EDIT : LOL ive been getting so much RMD’s (Random Moronic Downvotes) that i’m unable to vote you guys up
//////////////////////////////
var CSVDatei : TextAsset; //
var Zeilen; // File Handling.
var Werte; //
//////////////////////////////
///////////////////////////////////////
var Geschwindigkeit : GameObject; //
var Umdrehungen : GameObject; //
var Motorhitze : GameObject; // Analoge Anzeigen.
var Tankanzeige : GameObject; //
///////////////////////////////////////
//////////////
var KMH; //
var Gang; // Digitale Anzeigen.
var PCM; //
//////////////
//////////////////////////////////////////
var Geschwindigkeit_pos : float; //
var Umdrehungen_pos : float; // Positionswerte für Zeiger.
var Motorhitze_pos : float; //
var Tankanzeige_pos : float; //
//////////////////////////////////////////
var i : int = 1; // Zähler für Leseschleife
var p : int = 1; // Zähler für Zuweisungsschleife
function lese_datei() // Öffnen und Parsen der Datei.
{
for(i=1;i < 100; i++)
{
Zeilen = CSVDatei.text.Split("
“[0]); // Parsen an Zeilenendekennung
Werte = Zeilen*.Split(”;"[0]); // Parsen am Semikolon*
for(p = 1; p<100; p++)*
{*
var Geschwindigkeit_rotation = Werte[4];*
var Umdrehungen_rotation = Werte[9];*
var Motorhitze_rotation = Werte[8];*
print(Geschwindigkeit_rotation + "
" + Umdrehungen_rotation + "
" + Motorhitze_rotation);*
}*
}*
}*
Here u go. Code.