Reputation: 85
I am making a profile page for a matrimonial website. I am using these functions in make profile page .Is there any way to optimize these functions? They are called under not IsPostBack condition in Page Load Event of ASP.NET page and there are too many iterations.
private void LoadTime()
{
string text = string.Empty;
//hour
for (int i = 0; i < 12; i++)
{
text = (i + 1 < 10) ? "0" + (i + 1).ToString() : (i + 1).ToString();
ListItem li = new ListItem(text, i.ToString());
ddlHour.Items.Add(li);
}
//min
for (int i = 0; i < 60; i++)
{
text = (i < 10) ? "0" + i.ToString() : i.ToString();
ListItem li = new ListItem(text, i.ToString());
ddlMin.Items.Add(li);
}
}
private int loadfromage()
{
int from = (rbtnMale.Checked && !rbtnFemale.Checked) ? 18 : 21;
//from age
for (int i = from; i <= 49; i++)
{
string text = i.ToString();
ListItem li = new ListItem(text, i.ToString());
ddlFromAge.Items.Add(li);
}
return from;
}
private void loadtoage(int fromage)
{
//to age
for (int i = fromage; i <= 50; i++)
{
string text = i.ToString();
ListItem li = new ListItem(text, i.ToString());
ddlToAge.Items.Add(li);
}
}
private void loadfromheight()
{
//from height
for (int i = 4; i <= 7; i++)
{
string text = i.ToString();
ListItem li = new ListItem(text, i.ToString());
ddlppFromHeightFeet.Items.Add(li);
}
for (int i = 0; i <= 11; i++)
{
string text = i.ToString();
ListItem li = new ListItem(text, i.ToString());
ddlppFromHeightInches.Items.Add(li);
}
}
private int feetToInches(int feet, int inches)
{
return (feet * 12) + inches;
}
private void loadtoheight(int inches)
{
int feet = inches / 12;
inches %= 12;
//to height
for (int i = feet; i <= 8; i++)
{
string text = i.ToString();
ListItem li = new ListItem(text, i.ToString());
ddlppToHeightFeet.Items.Add(li);
}
for (int i = inches; i <= 11; i++)
{
string text = i.ToString();
ListItem li = new ListItem(text, i.ToString());
ddlppToHeightInches.Items.Add(li);
}
}
Upvotes: 2
Views: 488
Reputation: 419
private void loadtoage(int fromage)
{
//to age
for (int i = fromage; i <= 50; i++)
{
string text = i.ToString();// convert i to string just once. you are converting it again in next line. boxing and unboxing is expensive when it comes to performance.
ListItem li = new ListItem(text, i.ToString());//Rather than doing i.ToString() again, you can directly pass text. Its all same.
ddlToAge.Items.Add(li);
}
}
I see there are lots of places you are doing this.
Upvotes: 1