TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
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....
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();
// 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();
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();
//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();
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)
{
//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;
}
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)
{
//double che = distance(lat1, long1, lat2, long2, 'K');
//string newFormat = ;
sum += Convert.ToDouble((GetCorrectDistance(lat1, long1, lat2, long2)).ToString());
lat1 = lat2;
long1 = long2;
// double convertdist = Convert.ToDouble(d.Value);
lat2 = Convert.ToDouble(item.Lat);
long2 = Convert.ToDouble(item.Long);
}
}
if
(listgps.Count() > 0)
{
if
(lat1 != 0 && lat2 != 0)
{
// double che = distance(lat1, long1, lat2, long2, 'K');
//string newFormat = ;
//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;
}
Reply
Answers (
3
)
c sharp multthreading
string was not recognized as a valid datetime