Home  >  Article  >  php教程  >  C#、PHP与NodeJs性能测试对比

C#、PHP与NodeJs性能测试对比

WBOY
WBOYOriginal
2016-06-06 19:50:502029browse

namespace Benchmark { using System ; using System.Web ; using System.Web.UI ; using System.Threading.Tasks ; public partial class Default : System. Web . UI . Page { Randomrnd = null ; public void Page_Load ( object sender,EventArgse ) { /

namespace Benchmark

{

    using System;

    using System.Web;

    using System.Web.UI;

    using System.Threading.Tasks;

 

    public partial class Default : System.Web.UI.Page

    {

        Random rnd=null;

        public void Page_Load(object sender, EventArgs e)

        {

            //RegisterAsyncTask(new PageAsyncTask(PerformIOasync,PerformIOasync,null,null));

            rnd = new Random ();

            //Task.Run (new Action (PerformIOasync));

            PerformIOasync ();

        }

 

        public async void PerformIOasync()

        {

            string s=""; //generate a random string of 108KB and a random filename

            string fname = rndchar() + rndchar() + rndchar() + rndchar() +".txt";

            for(int i=0;i108000;i++)

            {

                char ch=rndchar();

                s += ch;

            }

 

            //write s to a file

            //file_put_contents($fname,$s);

 

            var slowTask1 =  Taskstring>.Factory.StartNew(()=> WriteToDisk(fname,s));

 

            await slowTask1;

 

            var SlowTask2 = Taskstring>.Factory.StartNew(()=> ReadFromDisk(fname,s));

 

            await SlowTask2;

 

            Response.Write(SlowTask2.Result.ToString());

        }

 

        private string WriteToDisk(string fname, string s){

            System.IO.File.WriteAllText (AppDomain.CurrentDomain.BaseDirectory + fname, s);

            return "";

        }

 

        private string ReadFromDisk(string fname, string s){

            return System.IO.File.ReadAllText (AppDomain.CurrentDomain.BaseDirectory + fname);

        }

 

        private char rndchar()

        {

            return (char)rnd.Next (65112);

        }

    }

}

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn