Results 1 to 10 of 19
-
18th Jun 2010, 12:23 PM #1OPლ(ಠ益ಠლ)Website's:
extremecoderz.com[c#] CHALLENGE - Beat the time!
Ok.
So Hyperz and i were discussing the finer points of multi-threading, asynchronous threads and all that story.
We decided to carry out a friendly "code-off" and see who could come up with the fastest solution based on how you concluded the best way to go about doing such a thing:
The task is simple: Perform a series of (pre-decided) http requests and time the request responses. The fastest code wins. Simple. We are also using a HTTP time-out of 5000 milliseconds, to avoid dead/slow servers from hampering the results.
URL LIST: http://www.coding.extremecoderz.com/urls.txt
Let the battle commence! In a FRIENDLY way! Please DO NOT flame if you get pwnt, this is in the interest of fun and learning.
Please Also Note: We are using a console application, not a windows Forms Application.
jayfella Reviewed by jayfella on . [c#] CHALLENGE - Beat the time! Ok. So Hyperz and i were discussing the finer points of multi-threading, asynchronous threads and all that story. We decided to carry out a friendly "code-off" and see who could come up with the fastest solution based on how you concluded the best way to go about doing such a thing: The task is simple: Perform a series of (pre-decided) http requests and time the request responses. The fastest code wins. Simple. We are also using a HTTP time-out of 5000 milliseconds, to avoid dead/slow Rating: 5
-
18th Jun 2010, 12:28 PM #2OPლ(ಠ益ಠლ)Website's:
extremecoderz.comMY code:
Im not a man of the book. I just write it how i see it. This probably goes against every rule in the programming book. Still. Its fast as fuck.
Basically what im doing here is throwing async out the window. I chuck everything into one huge array and throw it at a threadpool, which houses an array of HTTP-Repquests. Simplicity ftw...?
PHP Code:using System;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading;
using System.Net;
namespace ConsoleApplication1
{
class Program
{
public static string[] myUrls;
// HTTP arrays
static HttpWebRequest[] webReq;
static HttpWebResponse[] webResp;
static ManualResetEvent[] resetEvents;
static Stopwatch sw = new Stopwatch();
static void Main(string[] args)
{
sw.Start();
Console.WriteLine("starting...");
RunTest();
}
public static void RunTest()
{
string s = "";
using (StreamReader rdr = File.OpenText("urls.txt"))
{
s = rdr.ReadToEnd();
}
myUrls = s.Split('\n');
// Set Min-Max Threads (setting Both the same removes time-to-create delays)
ThreadPool.SetMinThreads(100, 100);
ThreadPool.SetMaxThreads(100, 100);
// Declare array Sizes Before starting...
DeclareArrays();
for (int i = 0; i < myUrls.Length; i++)
{
resetEvents[i] = new ManualResetEvent(false);
ThreadPool.QueueUserWorkItem(new WaitCallback(DoRequest), (object)i);
}
Console.Read();
}
public static void DeclareArrays()
{
webReq = new HttpWebRequest[myUrls.Length];
webResp = new HttpWebResponse[myUrls.Length];
resetEvents = new ManualResetEvent[myUrls.Length];
}
public static void DoRequest(object o)
{
int i = (int)o;
try
{
webReq[i] = (HttpWebRequest)WebRequest.Create(myUrls[i]);
webReq[i].Proxy = HttpWebRequest.GetSystemWebProxy();
webReq[i].ServicePoint.Expect100Continue = false;
webReq[i].AllowAutoRedirect = true;
webReq[i].KeepAlive = false;
webReq[i].UserAgent = "Mozilla Firefox";
webReq[i].Method = "GET";
webReq[i].Timeout = 5000;
webReq[i].ContentType = "application/x-www-form-urlencoded";
// Gimme some sugar!
webResp[i] = (HttpWebResponse)webReq[i].GetResponse();
Console.WriteLine(sw.Elapsed + " - Finsihed: " + myUrls[i]);
}
catch
{
// In case of fail, let the user know.
Console.WriteLine(sw.Elapsed + " - FAILED: " + myUrls[i]);
resetEvents[i].Set();
return;
}
// All Done. Tell the ThreadPool its finished so it can be re-used.
resetEvents[i].Set();
}
}
}
-
18th Jun 2010, 02:19 PM #3Respected DeveloperPHP Code:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApplication1
{
class Program
{
public static Stopwatch sw = new Stopwatch();
static void Main(string[] args)
{
Console.WriteLine("Starting");
ServicePointManager.Expect100Continue = false;
Thread.CurrentThread.Priority = ThreadPriority.AboveNormal;
ServicePointManager.DefaultConnectionLimit = 4;
RunTest();
Console.Read();
}
public static void RunTest()
{
string filename = "urls.txt";
string[] urls = File.ReadAllLines(filename);
sw.Start();
foreach (string url in urls) DoRequest(url);
}
public static void DoRequest(string url)
{
var thread = new Thread(state =>
{
var req = (HttpWebRequest)WebRequest.Create(state as String);
req.AllowAutoRedirect = false;
req.Timeout = 5000;
req.Proxy = null;
req.KeepAlive = false;
req.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");
try
{
using (var resp = (HttpWebResponse)req.GetResponse())
{
resp.Close();
//req.Abort();
Console.WriteLine("{0} - Finished: {1}", sw.Elapsed, resp.ResponseUri.OriginalString);
}
}
catch
{
req.Abort();
Console.WriteLine("{0} - FAILED: {1}", sw.Elapsed, req.RequestUri.OriginalString);
}
});
thread.Priority = ThreadPriority.Highest;
thread.IsBackground = true;
thread.Start(url);
}
/*public static void DoRequest(string url)
{
var req = (HttpWebRequest)WebRequest.Create(url);
req.AllowAutoRedirect = false;
req.Timeout = 5000;
req.Proxy = null;
req.KeepAlive = false;
req.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");
var async = req.BeginGetResponse(new AsyncCallback(result =>
{
var request = (HttpWebRequest)result.AsyncState;
try
{
using (var resp = (HttpWebResponse)request.EndGetResponse(result))
{
resp.Close();
request.Abort();
Console.WriteLine("{0} - Finished: {1}", sw.Elapsed, resp.ResponseUri.OriginalString);
}
}
catch
{
request.Abort();
Console.WriteLine("{0} - FAILED: {1}", sw.Elapsed, request.RequestUri.OriginalString);
}
}), req);
ThreadPool.RegisterWaitForSingleObject(async.AsyncWaitHandle, new WaitOrTimerCallback((state, timeout) =>
{
var request = (HttpWebRequest)state;
if (timeout)
{
request.Abort();
Console.WriteLine("{0} - FAILED: {1}", sw.Elapsed, request.RequestUri.OriginalString);
}
}), req, 5000, true);
}*/
}
}
RESULT: 5.183 seconds.
Using the propper async way, which is less resource intensive but slow I get around 17 seconds. That would be the part in the code that's commented out.
F# version:
Code:open System open System.Collections.Generic open System.Diagnostics open System.IO open System.IO.Compression open System.Linq open System.Net open System.Text open System.Threading open System.Threading.Tasks stdout.WriteLine "Starting." let urls = File.ReadAllLines "urls.txt" |> List.ofArray let sw = new Stopwatch() ServicePointManager.Expect100Continue <- false GC.Collect(); ServicePointManager.DefaultConnectionLimit <- 480 ServicePointManager.MaxServicePoints <- 480 ThreadPool.SetMaxThreads(480, 480) |> ignore ThreadPool.SetMinThreads(450, 450) |> ignore let DoRequest url = async { try let req = WebRequest.Create(url:string) :?> HttpWebRequest req.AllowAutoRedirect <- false req.Timeout <- 5000 req.Proxy <- null req.KeepAlive <- false req.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate") use! rsp = req.AsyncGetResponse() rsp.Close() String.Format("{0} - Finished: {1}", sw.Elapsed, url) |> stdout.WriteLine with _ -> String.Format("{0} - FAILED: {1}", sw.Elapsed, url) |> stdout.WriteLine return true } sw.Start() urls |> List.map DoRequest |> Async.Parallel |> Async.RunSynchronously |> ignore stdout.WriteLine "Done." stdin.Read() |> ignore
-
18th Jun 2010, 02:38 PM #4Member
I don't know if it's Hyperz pwned Jay or F# pwned C#. Either way, I win.
-
18th Jun 2010, 02:54 PM #5OPლ(ಠ益ಠლ)Website's:
extremecoderz.com
-
18th Jun 2010, 02:57 PM #6The Wise OneWebsite's:
twilight.ws ddlrank.comIsn't the speed of your internet connection the big unknown factor in this?
I can always be contacted by sending a tweet @twilightws
-
18th Jun 2010, 02:58 PM #7Respected Developer
We test both programs on our pc
.
-
18th Jun 2010, 02:59 PM #8OPლ(ಠ益ಠლ)Website's:
extremecoderz.comPHP Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading;
using System.Net;
namespace ConsoleApplication1
{
class Program
{
public static string[] myUrls;
static HttpWebRequest[] webReq;
static HttpWebResponse[] webResp;
static ManualResetEvent[] resetEvents;
static Stopwatch sw = new Stopwatch();
static void Main(string[] args)
{
Console.WriteLine("starting...");
RunTest();
}
public static void RunTest()
{
string s = "";
using (StreamReader rdr = File.OpenText("urls.txt"))
{ s = rdr.ReadToEnd(); }
myUrls = s.Split('\n');
ThreadPool.SetMinThreads(512, 512);
ThreadPool.SetMaxThreads(512, 512);
DeclareArrays();
sw.Start();
for (int i = 0; i < myUrls.Length; i++)
{
resetEvents[i] = new ManualResetEvent(false);
ThreadPool.QueueUserWorkItem(new WaitCallback(DoRequest), (object)i);
}
Console.Read();
}
public static void DeclareArrays()
{
webReq = new HttpWebRequest[myUrls.Length];
webResp = new HttpWebResponse[myUrls.Length];
resetEvents = new ManualResetEvent[myUrls.Length];
}
public static void DoRequest(object o)
{
int i = (int)o;
try
{
webReq[i] = (HttpWebRequest)WebRequest.Create(myUrls[i]);
webReq[i].Proxy = HttpWebRequest.GetSystemWebProxy();
webReq[i].Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate");
webReq[i].ServicePoint.Expect100Continue = false;
webReq[i].AllowAutoRedirect = false;
webReq[i].KeepAlive = false;
webReq[i].UserAgent = "Mozilla Firefox";
webReq[i].Method = "GET";
webReq[i].Timeout = 5000;
webReq[i].ContentType = "application/x-www-form-urlencoded";
webReq[i].AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
using (webResp[i] = (HttpWebResponse)webReq[i].GetResponse())
{
// webResp[i].Close();
Console.WriteLine(sw.Elapsed + " - Finsihed: " + myUrls[i]);
resetEvents[i].Set();
return;
}
}
catch
{
// webResp[i].Close();
Console.WriteLine(sw.Elapsed + " - FAILED: " + myUrls[i]);
resetEvents[i].Set();
return;
}
}
}
}
-
18th Jun 2010, 03:02 PM #9MemberWebsite's:
securewarez.netHyperz you tryhard!
-
20th Jun 2010, 10:35 PM #10MemberWebsite's:
bypassx.comOn jayfella?s code, exists one line not needed using GET method:
Code:webReq[i].ContentType = "application/x-www-form-urlencoded";
Anyways... speed results can be confused. All depends on how many timeouts the day of the test, the speed and the load of the server the day of the test.... etc...
Anyways are good pieces of code.
Sponsored Links
Thread Information
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)
Similar Threads
-
A Google +-challenge?
By Daniel in forum General DiscussionReplies: 2Last Post: 17th Jul 2011, 08:30 PM -
Challenge to GFX Genius !
By softleaks in forum Graphics AreaReplies: 21Last Post: 28th May 2011, 09:01 AM -
[c#] CHALLENGE - Beat the time! #2
By Hyperz in forum Web Development AreaReplies: 22Last Post: 22nd Jun 2010, 02:36 PM -
I Challenge U ALL
By Chief-Editor in forum General DiscussionReplies: 19Last Post: 2nd Mar 2010, 08:46 AM
themaCreator - create posts from...
Version 3.45 released. Open older version (or...