I have used 3 or 4 for each loop my code, as well as one internet URL, hit in one loop.
I have used Asynchronous in my code but it same time required for execution.
how to optimize execution time in below code suggest me....
- public string GetCorrectDistance(double lat1,double long1,double lat2,double long2)
- {
- string s;
- string d = null;
-
-
- using (WebClient client = new WebClient())
- {
- s = client.DownloadString("http://yournavigation.org/api/dev/route.php?flat=" + lat1 + "&flon=" + long1 + "&tlat=" + lat2 + "&tlon=" + long2 + "&v=motorcar&fast=1&instructions=1");
- }
- XmlDocument _doc = new XmlDocument();
- _doc.LoadXml(s);
- XmlNodeList _fnames = _doc.GetElementsByTagName("distance");
- for (int _i = 0; _i < _fnames.Count; ++_i)
- {
- d = _fnames[_i].InnerText;
- }
-
- return d;
- }
- public async Task<ActionResult> DateWLocationKM(int UserId =0)
- {
-
-
-
- var _checlAuth = this.CheckAuthentication();
- if (_checlAuth != null)
- {
- return _checlAuth;
- }
- ViewModel.MobileGPSLocation.GPSDateW gps = new ViewModel.MobileGPSLocation.GPSDateW();
-
- List<ViewModel.MobileGPSLocation.GPSDateW> listsample = new List<ViewModel.MobileGPSLocation.GPSDateW>();
- List<ViewModel.MobileGPSLocation.MobileGPSLocation> listgps = new List<ViewModel.MobileGPSLocation.MobileGPSLocation>();
-
- string lastmonth = Repository.IndainTime.getCreatedDate().Date.AddMonths(-1).ToString();
-
- string df =DateTime.Now.Date.ToString("yyyy-MM-dd");
- string dg = Convert.ToDateTime(lastmonth).Date.ToString("yyyy-MM-dd");
- var ccheckdata = GETAllLocation(UserId).Where(a => a.UserId == UserId && Convert.ToDateTime(a.CreatedDate).Date >= Convert.ToDateTime(dg).Date && Convert.ToDateTime(a.CreatedDate).Date <= Convert.ToDateTime(df).Date).OrderByDescending(a => a.Id).ToList().Select(a=>Convert.ToDateTime(a.CreatedDate).Date).Distinct();
-
- foreach (var item in ccheckdata)
- {
- ViewModel.MobileGPSLocation.GPSDateW model = new ViewModel.MobileGPSLocation.GPSDateW();
- double d =await ListGPS(item.Date.ToString(), item.Date.ToString(), UserId);
- model.dateTime = item.Date.ToString();
- model.countkM = d;
- model.Id = UserId;
- listsample.Add(model);
- }
- gps.list = listsample;
- gps.Id = UserId;
- return View(gps);
- }
- public async Task<double> ListGPS(string date, string endDate, int Id)
- {
-
- sum = 0;
- List<ViewModel.MobileGPSLocation.MobileGPSLocation> listgps = new List<ViewModel.MobileGPSLocation.MobileGPSLocation>();
- string area = null;
- var ccheckdata = listGPSLocation.Where(a => a.UserId == Id && Convert.ToDateTime(a.CreatedDate).Date >= Convert.ToDateTime(date).Date && Convert.ToDateTime(a.CreatedDate).Date <= Convert.ToDateTime(endDate).Date).OrderByDescending(a => a.Id).ToList();
- #region
- double lat1 = 0;
- double lat2 = 0;
- double long1 = 0;
- double long2 = 0;
- foreach (var s in ccheckdata)
- {
- ViewModel.MobileGPSLocation.MobileGPSLocation ss = new ViewModel.MobileGPSLocation.MobileGPSLocation();
- if (area == null)
- {
- area = s.Address;
- area = s.Address;
- ss.Lat = s.Lat;
- ss.Long = s.Long;
- ss.Id = s.Id;
- ss.Address = s.Address;
- listgps.Add(ss);
-
- }
- else if (area == s.Address)
- {
-
-
-
-
-
- area = s.Address;
- }
- else
- {
- ss.Lat = s.Lat.Trim();
- ss.Long = s.Long.Trim();
- ss.Id = s.Id;
- ss.Address = s.Address.Trim();
- listgps.Add(ss);
- area = s.Address;
-
- }
-
- }
-
- #endregion
- #region
- foreach (var item in listgps.OrderBy(a => a.Id))
- {
- if (lat1.Equals(0))
- {
-
- lat1 = Convert.ToDouble(item.Lat);
- long1 = Convert.ToDouble(item.Long);
- }
- else if (lat2.Equals(0))
- {
- lat2 = Convert.ToDouble(item.Lat);
- long2 = Convert.ToDouble(item.Long);
- }
- else if (lat1 != 0 && lat2 != 0)
- {
-
-
-
- sum += Convert.ToDouble((GetCorrectDistance(lat1, long1, lat2, long2)).ToString());
-
- lat1 = lat2;
- long1 = long2;
-
- lat2 = Convert.ToDouble(item.Lat);
- long2 = Convert.ToDouble(item.Long);
- }
-
- }
-
- if (listgps.Count() > 0)
- {
- if (lat1 != 0 && lat2 != 0)
- {
-
-
- //below function one url hit each time then we will get output ...this url intenet is required.
- sum += Convert.ToDouble((GetCorrectDistance(lat1, long1, lat2, long2)).ToString());
-
- }
- }
- #endregion
-
-
-
- return sum;
- }