string[] myhtmlNum = new string[titleList.Count];            try
            {
                for (int i = 0; i < titleList.Count; i++)
                {
                    try
                    {
                        string HtmlName = DateTime.Now.Year.ToString() + DateTime.Now.Month + DateTime.Now.Minute + DateTime.Now.Day + DateTime.Now.Minute + DateTime.Now.Second + DateTime.Now.Millisecond + rad.Next(9999);
                        sr = new StreamReader(Server.MapPath("../../news/ArticleInfoHtml.htm"));
                        sw = new StreamWriter(Server.MapPath("../../news/xw/" + HtmlName + ".html"), false, System.Text.Encoding.GetEncoding("utf-8"));
                        string Html = sr.ReadToEnd();                        string sql2 = "SELECT top 9 [CategoriesId],[Name] FROM [WuHan].[dbo].[Categories]";
                        using (sdr = DbHelperSQL.ExecuteReader(sql2))
                        {
                            for (int n = 1; n <= 9; n++)
                            {
                                if (sdr.Read())
                                {
                                    Html = Html.Replace("html_menu_" + n, sdr["Name"].ToString());
                                    Html = Html.Replace("zxdx_" + n, sdr["CategoriesId"].ToString());
                                }
                            }
                        }                        Html = Html.Replace("title_name", titleList[i].Titlename);
                        Html = Html.Replace("title_time", titleList[i].Time);
                        Html = Html.Replace("title_text", titleList[i].Text);
                        sw.Write(Html);
                        Response.Write("生成成功!编号:" + i + "<br>");
                        myhtmlNum[i] = HtmlName;
                        string sql = "UPDATE [Title] SET [HtmlNum] = " + myhtmlNum[i].ToString() + " WHERE [TitleId]=" + titleList[i].Titleid + "";
                        DbHelperSQL.ExecuteSql(sql);
                    }
                    catch (Exception ex)
                    {
                        Response.Write(ex);
                    }
                    finally
                    {
                        sw.Flush();
                        sw.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex);
            }
            finally
            {            }谁能帮我优化下.....感谢 自己代码写的太烂 也不知道如何改 
生成静态化。 占用CPU使用量太高了。 站到60%半天缓不过神来。 
生成数据量1w5千条的样子....

解决方案 »

  1.   

    string Html = sr.ReadToEnd();用StringBuilder替换String类型,尤其是字符串内容需要经常修改变化时,不要用String!
      

  2.   

    另外,用sql语句就可以实现拼装字符串了,建议你在存储过程中实现拼装,而不是读取到本地进行页面内容的拼装,这样调用一个存储过程,就直接返回实际要写入的静态页面的数据了,速度肯定会有质的提高。
      

  3.   

    去掉  try    catch 不需要嵌套那么多try
      

  4.   

    静态化
    protected override void Render(HtmlTextWriter writer) {   
      StreamWriter r=new StreamWriter(Server.MapPath(""), false,System.Text.Encoding.UTF8);   
      HtmlTextWriter h=new HtmlTextWriter(r);   
      base.Render(h);   
      r.Close();   
      h.Close();   
      }   
     StringWriter wr = new StringWriter();
     Server.Execute("", wr);   
     this.lit.Text = Server.HtmlEncode(wr.ToString());
     File.WriteAllText(Server.MapPath(""), wr.ToString());   
     
    http://topic.csdn.net/u/20100512/12/e09fc32d-a030-43cd-bdee-579ffb7f8469.html?26869
      

  5.   


    static ArrayList NicheInfoID = new ArrayList();//会员的商机详细信息ID集合
    protected void Page_Load(object sender, EventArgs e)
            {
                if (Session["AdminID"] == null)
                {
                    Response.Write("<script>window.open('/Admin/Logout.aspx','_parent')</script>");
                }
                if (!IsPostBack)
                {
                    databind();
                }
            }
     protected void databind()
            {
                
                //以下保存需要静态的商机信息
                NicheInfoID.Clear();//先清空一下,因为变量时静态的
                Maticsoft.BLL.YZ_MemberNicheRelation MemberNicheBll = new Maticsoft.BLL.YZ_MemberNicheRelation();
                DataSet MemberNicheDs = MemberNicheBll.GetList("");//查出会员发的商机信息
                //保存需要生成静态页的NicheID
                for (int i = 0; i < MemberNicheDs.Tables[0].Rows.Count; i++)
                {
                    //只生成审核通过的并且未锁定的
                    Maticsoft.BLL.YZ_NicheInfo NicheBll = new Maticsoft.BLL.YZ_NicheInfo();
                    Maticsoft.Model.YZ_NicheInfo NicheModel = NicheBll.GetModel(Convert.ToInt32(MemberNicheDs.Tables[0].Rows[i]["NicheID"]));
                    if (NicheModel.IsCheck == 1 && NicheModel.IsLock == false)
                    {
                        NicheInfoID.Add(MemberNicheDs.Tables[0].Rows[i]["NicheID"]);
                    }
                }
            }
    //根据URL生成静态页面
            protected void tohtml(string Url, int ID, string type)
            {
                Encoding code = Encoding.GetEncoding("utf-8");
                StreamWriter sw = null;
                string str = null;            //读取远程路径
                System.Net.WebClient wc = new System.Net.WebClient();
                Byte[] pageData = wc.DownloadData(Url);
                str = System.Text.Encoding.UTF8.GetString(pageData);
                string fileName = "Niche_" + ID + ".html";
                if (type == "news")//新闻页的名称
                {
                    fileName = "News_" + ID + ".html";
                }
                if (type == "product")//产品页的名称
                {
                    fileName = "Product_" + ID + ".html";
                }
                if (type == "index")//会员公司的主页
                {
                    fileName = "index.html";
                }
                if (type == "intro")//公司简介页
                {
                    fileName = "CompanyIntroduction.html";
                }
                if (type == "contact")//公司联系方式页
                {
                    fileName = "ContactWay.html";
                }
                //写入
                try
                {
                    if (!Directory.Exists(Server.MapPath("/html/sys/")))//判断文件夹是否存在
                    {
                        Directory.CreateDirectory(Server.MapPath("/html/sys/")); //创建文件夹
                    }
                    sw = new StreamWriter(Server.MapPath("/html/sys/") + fileName, false, code);
                    sw.Write(str);
                    sw.Flush();            }
                catch (Exception ex)
                {
                    throw ex;
                }
                finally
                {
                    sw.Close();
                }
            }
    protected void Button2_Click(object sender, EventArgs e)
            {
                this.lbInfo.Text = "静态化页面已启动请稍等...";
                //遍历需要生成静态页面的商机集合依次生成
                for (int i = 0; i < NicheInfoID.Count; i++)
                {
                    this.lbInfo.Text = "正在静态化您发布的并且通过审核的商机信息...";
                    string NicheInfoUrl = "http://域名/NicheInfo.aspx?NicheID=" + Convert.ToInt32(NicheInfoID[i]);
                    tohtml(NicheInfoUrl, Convert.ToInt32(NicheInfoID[i]), "niche");
                }
                this.lbInfo.Text = "商机信息静态化完成";
                Page.ClientScript.RegisterStartupScript(GetType(), "", "<script type='text/javascript'>alert('静态化页面已完成!');</script>");
            }我用的根据url生成,先在51aspx上下的一个例子,又改了改就这样
      

  6.   

    try catch也会增加负担,如果没必要的时候建议不要用
    如果字符串很长,建议用StringBuilder
                            string sql = "UPDATE [Title] SET [HtmlNum] = " + myhtmlNum[i].ToString() + " WHERE [TitleId]=" + titleList[i].Titleid + "";
                            DbHelperSQL.ExecuteSql(sql);
    可以换成存储过程!
      

  7.   

    我看了一下,建议都在1,2,3,楼给完了,呵呵.
    没发现什么问题哦,就是感觉你的循环量大了点,占CPU也很正常嘛