Jump to content
MakeWebGames

ASP.NET vs PHP - Speed check


a_bertrand

Recommended Posts

You may test it by yourself:

http://base.nowhere-else.org/temp/speedtest/Default.aspx

http://base.nowhere-else.org/temp/speedtest/speedtest.php

My results:

ASP.NET on Mono (linux): 3.8295770

PHP: 4.037904

The codes:

<?php
header("Expires: now");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
   <title>PHP Speed test</title>
</head>
<body>
   <form id="form1" runat="server" method="POST">
   <div>
<?PHP
function microtime_float()
{
   list($usec, $sec) = explode(" ", microtime());
   return ((float)$usec + (float)$sec);
}

if($_SERVER["REQUEST_METHOD"] == "POST")
{
$start=$_POST["START"];
$time_start=$_POST["MICRO"];
$pass=$_POST["COUNTER"]+1;
$time_end = microtime_float();
$time = $time_end - $time_start;	
echo "<INPUT TYPE=HIDDEN NAME=COUNTER VALUE=".$pass." ID=COUNTER>";
echo "<INPUT TYPE=HIDDEN NAME=START VALUE=".$start." ID=START>";
echo "<INPUT TYPE=HIDDEN NAME=MICRO VALUE=".$time_start." ID=START>";
echo "Start time: ".strftime("%H:%M:%S",$start)."
";
echo "Pass: ".$pass." / 50
";
echo "Ellapsed time: ".$time." sec
";
echo "<SELECT STYLE='width:200px' SIZE='10'>";
for($i=0;$i < 1000;$i++)
{
	echo "<OPTION>".sin($i/10.0+$pass)."\n";
}
echo "</SELECT>";
}
else
{
echo "<INPUT TYPE=HIDDEN NAME=COUNTER VALUE=0 ID=COUNTER>";
echo "<INPUT TYPE=HIDDEN NAME=START VALUE=".time()." ID=START>";
echo "<INPUT TYPE=HIDDEN NAME=MICRO VALUE=".microtime_float()." ID=START>";
}
?>
   </div>
   </form>
   <script type="text/javascript">
       var count = parseInt(document.getElementById('COUNTER').value);
       if (count < 50)
           document.forms[0].submit();
   </script>
</body>
</html>

 

ASPx:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="speedTest.Default" EnableEventValidation="false" EnableViewState="false" EnableSessionState="False" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
   <title>ASP.NET Speed test</title>
</head>
<body>
   <form id="form1" runat="server" EnableViewState="false">
   <div>
       <asp:HiddenField ID="hdCounter" runat="server" Value="0" EnableViewState="False" />
       <asp:HiddenField ID="startTime" runat="server" Value="0" EnableViewState="False" />
       <asp:Label ID="lblStartTime" runat="server" EnableViewState="False" />

       <asp:Label ID="lblEllapsed" runat="server" EnableViewState="False" />

       <asp:Label ID="lblStatus" runat="server" Text="Pass 0 / 50"  EnableViewState="False"/>

       <asp:ListBox ID="lstData" runat="server" Width="200px" Rows="10" EnableViewState="False">
       </asp:ListBox>
   </div>
   </form>
   <script type="text/javascript">
       var count = parseInt(document.getElementById('hdCounter').value);
       if (count < 50)
           document.forms[0].submit();
   </script>
</body>
</html>

 

The C# part:

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace speedTest
{
   public partial class Default : System.Web.UI.Page
   {
       protected override void OnInit(EventArgs e)
       {
           base.OnInit(e);
           this.EnableViewState = false;
       }

       protected void Page_Load(object sender, EventArgs e)
       {
           Response.Expires = -1;
           if (Page.IsPostBack)
           {
               int pass = int.Parse(Request["hdCounter"]);
               DateTime start = DateTime.FromBinary(long.Parse(Request["startTime"]));
               startTime.Value = Request["startTime"];
               hdCounter.Value = (pass + 1).ToString();
               lblStatus.Text = "Pass " + hdCounter.Value + " / 50";
               lblStartTime.Text = "Start: " + start.ToString("G");
               lblEllapsed.Text = "Ellapsed: " + (DateTime.Now - start).ToString();

               for (int i = 0; i < 1000; i++)
               {
                   double a = Math.Sin(i / 10.0 + pass);
                   lstData.Items.Add(a.ToString());
               }
           }
           else
           {
               lblStartTime.Text = "Start: "+DateTime.Now.ToString("G");
               startTime.Value = DateTime.Now.ToBinary().ToString();
           }
       }
   }
}

 

To note that ASP.NET on mono is certainly not the fastest. I ran both on the same machine to avoid any kind of machine / os comparison. I avoided as much as possible all the ASP.NET goodies like viewstate as well as eventstate (viewstate is like a browser side session, where eventstate checks that nobody touched your stuff in the meantime) to be as fair as possible as those would have slowed down of course. I used however the ASP controls and not the HTML controls which may have been a bit faster. Also I didn't wrote the code in ASP like in PHP which would have been possible but you would then loose all the nice code behind trick.

So what does that say? ASP.NET is certainly not slower than PHP, even running on linux. As info, on my windows server (which is smaller than my linux server) I get 1.84 sec for the same operation.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...