I wrote some simple code to grab some key values from a json string, but when i go to multi thread it, to make it quickly grab the value multiple times, it gets slower each time. Im not quite sure why this is happening, i was thinking it might have had something to do with my cpu? Adding a wait function seems to fix it, but makes it go extremely slow, im out of ideas and im not sure as to what is causing it.
Output:
Single Threaded
598
Elapsed=0.558
598
Elapsed=0.121
598
Elapsed=0.127
598
Elapsed=0.127
598
Elapsed=1.167
598
Elapsed=0.122
598
Elapsed=0.124
598
Elapsed=0.145
598
Elapsed=0.146
598
Elapsed=0.12
------------------------------
Multi Threading
598
Elapsed=0.129
598
Elapsed=0.179
598
Elapsed=0.227
598
Elapsed=0.299
598
Elapsed=0.326
598
Elapsed=0.396
598
Elapsed=0.427
598
Elapsed=0.505
598
Elapsed=0.525
598
Elapsed=0.703
private static void Test2() {
Stopwatch sw = new Stopwatch();
sw.Start();
using (WebClient wb = new WebClient()) {
wb.Proxy = null;
string data = wb.DownloadString("https://google.com");
sw.Stop();
Console.WriteLine("Elapsed={0}", (double)sw.ElapsedMilliseconds / 1000);
}
}
static void Main(string[] args) {
Console.WriteLine("Single Threaded");
Test2();
Test2();
Test2();
Test2();
Test2();
Test2();
Test2();
Test2();
Test2();
Test2();
Console.WriteLine("------------------------------");
Console.WriteLine("Multi Threading");
for (int i = 0; i < 10; i++) {
void Multi() {
Test2();
}
new Thread(Multi).Start();
}
Console.Read();
}