Skip to content

Instantly share code, notes, and snippets.

@sjehutch
Created August 8, 2019 22:54
Show Gist options
  • Select an option

  • Save sjehutch/bba8d0cdc332a09bbb5f4f4bc1328f2b to your computer and use it in GitHub Desktop.

Select an option

Save sjehutch/bba8d0cdc332a09bbb5f4f4bc1328f2b to your computer and use it in GitHub Desktop.
Comparing arrarylist Lists and int array
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
namespace myBlank {
public class Program {
public static void Main () {
long duration1 = MeasureA ();
long duration2 = MeasureB ();
long duration3 = MeasureC ();
System.Console.WriteLine ($"Arraylist {duration1}");
System.Console.WriteLine ($"List<t>{duration2}");
System.Console.WriteLine ($"Int [] {duration3}");
}
private const int nums = 100000000;
public static long MeasureA () {
Stopwatch stopwatch = new Stopwatch ();
stopwatch.Start ();
ArrayList arrary = new ArrayList ();
for (int i = 0; i < nums; i++) {
arrary.Add (i);
}
stopwatch.Stop ();
return stopwatch.ElapsedMilliseconds;
}
public static long MeasureB () {
Stopwatch stopwatch = new Stopwatch ();
stopwatch.Start ();
List<int> arr2 = new List<int> ();
for (int i = 0; i < nums; i++) {
arr2.Add (i);
}
stopwatch.Stop ();
return stopwatch.ElapsedMilliseconds;
}
public static long MeasureC () {
Stopwatch stopwatch = new Stopwatch ();
stopwatch.Start ();
int[] array4 = new int[nums];
for (int i = 0; i < nums; i++) {
array4[i] = i;
}
stopwatch.Stop ();
return stopwatch.ElapsedMilliseconds;
}
public class Calc {
public static int Add (List<int> numbers) {
int sum = 0;
foreach (var nums in numbers) {
sum = sum + nums;
}
return sum;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment