Kalyani Shevale

Kalyani Shevale

  • NA
  • 3.2k
  • 705.1k

Execution time more Required in foreach loop

Aug 23 2019 6:17 AM
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....
 
  1. public string GetCorrectDistance(double lat1,double long1,double lat2,double long2)  
  2.       {  
  3.           string s;  
  4.           string d = null;  
  5.            
  6.   
  7.               using (WebClient client = new WebClient())  
  8.               {  
  9.                   s = client.DownloadString("http://yournavigation.org/api/dev/route.php?flat=" + lat1 + "&flon=" + long1 + "&tlat=" + lat2 + "&tlon=" + long2 + "&v=motorcar&fast=1&instructions=1");  
  10.               }  
  11.               XmlDocument _doc = new XmlDocument();  
  12.               _doc.LoadXml(s);  
  13.               XmlNodeList _fnames = _doc.GetElementsByTagName("distance");  
  14.               for (int _i = 0; _i < _fnames.Count; ++_i)  
  15.               {  
  16.                   d = _fnames[_i].InnerText;  
  17.               }  
  18.             
  19.             return d;  
  20.       }  
  21.       public async Task<ActionResult>  DateWLocationKM(int UserId =0)  
  22.       {  
  23.             
  24.   
  25.          
  26.               var _checlAuth = this.CheckAuthentication();  
  27.           if (_checlAuth != null)  
  28.           {  
  29.               return _checlAuth;  
  30.           }  
  31.           ViewModel.MobileGPSLocation.GPSDateW gps = new ViewModel.MobileGPSLocation.GPSDateW();  
  32.   
  33.           List<ViewModel.MobileGPSLocation.GPSDateW> listsample = new List<ViewModel.MobileGPSLocation.GPSDateW>();  
  34.           List<ViewModel.MobileGPSLocation.MobileGPSLocation> listgps = new List<ViewModel.MobileGPSLocation.MobileGPSLocation>();  
  35.            
  36.           string lastmonth = Repository.IndainTime.getCreatedDate().Date.AddMonths(-1).ToString();  
  37.           // var ccheckdata = WebApp.MvcApplication.listGPSLocation.Where(a => a.UserId == UserId && Convert.ToDateTime(a.CreatedDate).Date>=Convert.ToDateTime(lastmonth).Date ).OrderByDescending(a => a.Id).ToList().Select(a=>a.CreatedDate.Date).Distinct();  
  38.           string df =DateTime.Now.Date.ToString("yyyy-MM-dd");  
  39.           string dg = Convert.ToDateTime(lastmonth).Date.ToString("yyyy-MM-dd");  
  40.           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();  
  41.           //var ccheckdata = WebApp.MvcApplication.listGPSLocation.Where(a => a.UserId == UserId && Convert.ToDateTime(a.CreatedDate) >=Convert.ToDateTime(dg).Date  && Convert.ToDateTime(a.CreatedDate) <= Convert.ToDateTime(df).Date).ToList();  
  42.             foreach (var item in ccheckdata)  
  43.           {  
  44.               ViewModel.MobileGPSLocation.GPSDateW model = new ViewModel.MobileGPSLocation.GPSDateW();  
  45.               double d =await ListGPS(item.Date.ToString(), item.Date.ToString(), UserId);  
  46.               model.dateTime = item.Date.ToString();  
  47.               model.countkM = d;  
  48.               model.Id = UserId;  
  49.               listsample.Add(model);  
  50.           }  
  51.           gps.list = listsample;  
  52.           gps.Id = UserId;  
  53.           return View(gps);  
  54.       }  
  55.        public async Task<double> ListGPS(string date, string endDate, int Id)  
  56.       {  
  57.   
  58.           sum = 0;  
  59.           List<ViewModel.MobileGPSLocation.MobileGPSLocation> listgps = new List<ViewModel.MobileGPSLocation.MobileGPSLocation>();  
  60.           string area = null;  
  61.          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();  
  62.           #region  
  63.           double lat1 = 0;  
  64.           double lat2 = 0;  
  65.           double long1 = 0;  
  66.           double long2 = 0;  
  67.           foreach (var s in ccheckdata)  
  68.           {  
  69.               ViewModel.MobileGPSLocation.MobileGPSLocation ss = new ViewModel.MobileGPSLocation.MobileGPSLocation();  
  70.               if (area == null)  
  71.               {  
  72.                   area = s.Address;  
  73.                   area = s.Address;  
  74.                   ss.Lat = s.Lat;  
  75.                   ss.Long = s.Long;  
  76.                   ss.Id = s.Id;  
  77.                   ss.Address = s.Address;  
  78.                   listgps.Add(ss);  
  79.   
  80.               }  
  81.               else if (area == s.Address)  
  82.               {  
  83.                   //ss.Lat = s.Lat.Trim();  
  84.                   //ss.Long = s.Long.Trim();  
  85.                   //ss.Id = s.Id;  
  86.                   //ss.Address = s.Address.Trim();  
  87.                   //listgps.Add(ss);  
  88.                   area = s.Address;  
  89.               }  
  90.               else  
  91.               {  
  92.                   ss.Lat = s.Lat.Trim();  
  93.                   ss.Long = s.Long.Trim();  
  94.                   ss.Id = s.Id;  
  95.                   ss.Address = s.Address.Trim();  
  96.                   listgps.Add(ss);  
  97.                   area = s.Address;  
  98.   
  99.               }  
  100.   
  101.           }  
  102.  
  103.           #endregion  
  104.           #region  
  105.           foreach (var item in listgps.OrderBy(a => a.Id))  
  106.           {  
  107.               if (lat1.Equals(0))  
  108.               {  
  109.   
  110.                   lat1 = Convert.ToDouble(item.Lat);  
  111.                   long1 = Convert.ToDouble(item.Long);  
  112.               }  
  113.               else if (lat2.Equals(0))  
  114.               {  
  115.                   lat2 = Convert.ToDouble(item.Lat);  
  116.                   long2 = Convert.ToDouble(item.Long);  
  117.               }  
  118.               else if (lat1 != 0 && lat2 != 0)  
  119.               {  
  120.                   //double che = distance(lat1, long1, lat2, long2, 'K');  
  121.                   //string newFormat = ;  
  122.                     
  123.                   sum += Convert.ToDouble((GetCorrectDistance(lat1, long1, lat2, long2)).ToString());  
  124.   
  125.                   lat1 = lat2;  
  126.                   long1 = long2;  
  127.                   //  double convertdist = Convert.ToDouble(d.Value);  
  128.                   lat2 = Convert.ToDouble(item.Lat);  
  129.                   long2 = Convert.ToDouble(item.Long);  
  130.               }  
  131.   
  132.           }  
  133.   
  134.           if (listgps.Count() > 0)  
  135.           {  
  136.               if (lat1 != 0 && lat2 != 0)  
  137.               {  
  138.                   // double che = distance(lat1, long1, lat2, long2, 'K');  
  139.                   //string newFormat = ;  
  140. //below function one url hit each time then we will get output  ...this url intenet is required.
  141.                   sum += Convert.ToDouble((GetCorrectDistance(lat1, long1, lat2, long2)).ToString());  
  142.   
  143.               }  
  144.           }  
  145.           #endregion  
  146.   
  147.   
  148.   
  149.           return sum;  
  150.       }  
 

Answers (3)