patterncsharpMinor
Speeding up the process of obtaining data
Viewed 0 times
obtainingtheprocessspeedingdata
Problem
I created this script to obtain data from graphs online, from monavisaxmldata. I were wondering if there was a way to obtain the data faster. The program which uses this data gets presented in a presentation which takes roughly 5 minutes. But the data already takes 1.5 minutes to load. Which is quiet a bite in such a short time period.
Is the
```
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Net;
using System.Web;
public class PVPanels_Actueel : MonoBehaviour
{
private static WebClient myWebClient = new WebClient();
private List>> DataList = new List>>();
private List graphReadyData = new List();
public event System.Action> PVPanelsActDataParsed = delegate { };
private void Start()
{
myWebClient.DownloadStringCompleted += onDownloadComplete;
}
public static void StartDownload(int day, int month, int year)
{
string dateAndTime = string.Format("{0}-{1}-{2}+23:00", day, month, year);
MonavisaRequestForm myRequest = new MonavisaRequestForm
(
"foo",
"bar",
string.Format("http://www.monavisa.info/CreateGraphData?graphs=1&graph[0]=1226&todate={0}&period=1&step=0&b_id=194&inter=1&other_graph=true", dateAndTime),
ref myWebClient
);
MonavisaFetch.instance.getData(ref myRequest);
}
private void onDownloadComplete(object sender, DownloadStringCompletedEventArgs e)
{
DataList.Clear();
DataList = MonavisaParse.Parse(e.Result);
graphReadyData.Clear();
float totalKwh = 0;
float totalMw2 = 0;
string latestDate = "";
for (int i = 0; i 0)
{
totalMw2 += f0;
latestDate = DataList[0].Value[i].date;
}
if (f1 > 0 || f2 > 0 || f3 > 0)
{
totalKwh += f1 + f2 + f3;
}
}
graphReadyData.Add(new MonavisaGraphData { date = latestDate, va
Is the
WebClient() optimized in speed or should I approach the handling of the information differently? ```
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Net;
using System.Web;
public class PVPanels_Actueel : MonoBehaviour
{
private static WebClient myWebClient = new WebClient();
private List>> DataList = new List>>();
private List graphReadyData = new List();
public event System.Action> PVPanelsActDataParsed = delegate { };
private void Start()
{
myWebClient.DownloadStringCompleted += onDownloadComplete;
}
public static void StartDownload(int day, int month, int year)
{
string dateAndTime = string.Format("{0}-{1}-{2}+23:00", day, month, year);
MonavisaRequestForm myRequest = new MonavisaRequestForm
(
"foo",
"bar",
string.Format("http://www.monavisa.info/CreateGraphData?graphs=1&graph[0]=1226&todate={0}&period=1&step=0&b_id=194&inter=1&other_graph=true", dateAndTime),
ref myWebClient
);
MonavisaFetch.instance.getData(ref myRequest);
}
private void onDownloadComplete(object sender, DownloadStringCompletedEventArgs e)
{
DataList.Clear();
DataList = MonavisaParse.Parse(e.Result);
graphReadyData.Clear();
float totalKwh = 0;
float totalMw2 = 0;
string latestDate = "";
for (int i = 0; i 0)
{
totalMw2 += f0;
latestDate = DataList[0].Value[i].date;
}
if (f1 > 0 || f2 > 0 || f3 > 0)
{
totalKwh += f1 + f2 + f3;
}
}
graphReadyData.Add(new MonavisaGraphData { date = latestDate, va
Solution
It looks fast enough to me, nothing screams "major slowdown" in your code, so I would recommend putting a profiler on your code to spot any remaining hotpoints. After that, it's a case of damage control to try to reduce the impact of the data transfer time. As such, consider starting the download as early as possible.
On other matters:
Use var when the right hand side of your declaration makes the type obvious:
should be:
The reason for doing this is that should you change the type of the variable, you only have to change it in one place.
I'm also seeing a few magic strings and numbers that it might be wise to extract as constants or readonly instance variables.
I also don't like your short variable names for your float datapoints. Firstly, if there's nothing that distinguishes them other than the order in which you read them, store them as an array, although I suspect this is not the case, because f0 is given special treatment in the following code.
Secondly, to a maintenance programmer
I also note a couple of worrisome points about your implementation:
You don't seem to handle any potential errors at all. What if you can't make a connection, what if the connection drops half way through? Looking at your code literally nothing would happen.
You also only clear your
On other matters:
Use var when the right hand side of your declaration makes the type obvious:
string dateAndTime = string.Format("{0}-{1}-{2}+23:00", day, month, year);should be:
var dateAndTime = string.Format("{0}-{1}-{2}+23:00", day, month, year);The reason for doing this is that should you change the type of the variable, you only have to change it in one place.
I'm also seeing a few magic strings and numbers that it might be wise to extract as constants or readonly instance variables.
I also don't like your short variable names for your float datapoints. Firstly, if there's nothing that distinguishes them other than the order in which you read them, store them as an array, although I suspect this is not the case, because f0 is given special treatment in the following code.
Secondly, to a maintenance programmer
f1 means nothing, unless you're referring to a high-speed motor race. Call them dataPoints if you must, but f1 is extremely unhelpful.I also note a couple of worrisome points about your implementation:
You don't seem to handle any potential errors at all. What if you can't make a connection, what if the connection drops half way through? Looking at your code literally nothing would happen.
You also only clear your
graphReadyData variable when the download completes. If the download fails, this variable will contain the previous correctly-downloaded data. This has the potential to cause errors if not for yourself then for a maintenance programmer, so I would recommend clearing it when you begin the download.Context
StackExchange Code Review Q#68286, answer score: 2
Revisions (0)
No revisions yet.