C
C#3mo ago
SWEETPONY

✅ I don't undertand the error: capacity was less than the current size

Hey I have two versions of code and second one doesn't work first version:
int[] array = Enumerable.Range(0, 1000000000).ToArray();
var lst = new List<double>();

var stopWatch = new Stopwatch();
stopWatch.Start();

for (int i = 0; i < array.Length; i++)
{
var result = Math.Pow(i, 100000000000);
lst.Add(result);
}

stopWatch.Stop();
Console.WriteLine(stopWatch.Elapsed);
int[] array = Enumerable.Range(0, 1000000000).ToArray();
var lst = new List<double>();

var stopWatch = new Stopwatch();
stopWatch.Start();

for (int i = 0; i < array.Length; i++)
{
var result = Math.Pow(i, 100000000000);
lst.Add(result);
}

stopWatch.Stop();
Console.WriteLine(stopWatch.Elapsed);
second version:
int[] array = Enumerable.Range(0, 1000000000).ToArray();
var lst = new List<double>();

var stopWatch = new Stopwatch();
stopWatch.Start();

Parallel.For(0, array.Length, i =>
{
var result = Math.Pow(i, 100000000000);
lst.Add(result);
});

stopWatch.Stop();
Console.WriteLine(stopWatch.Elapsed);
int[] array = Enumerable.Range(0, 1000000000).ToArray();
var lst = new List<double>();

var stopWatch = new Stopwatch();
stopWatch.Start();

Parallel.For(0, array.Length, i =>
{
var result = Math.Pow(i, 100000000000);
lst.Add(result);
});

stopWatch.Stop();
Console.WriteLine(stopWatch.Elapsed);
second version always throws the exception: Unhandled exception. System.AggregateException: One or more errors occurred. (capacity was less than the current size. (Parameter 'value')) (capacity was less than the current size. (Parameter 'value')) (capacity was less than the current size. (Parameter 'value')) (capacity was less than the current size.
11 Replies
❤RieBi&❤
❤RieBi&❤3mo ago
You can't just use regular list in multiple threads and appends elements to it on different threads
SWEETPONY
SWEETPONYOP3mo ago
simple for takes 25 seconds in this case, that is why I decided to use Parallel.For but maybe I was wrong and it shouldn't be faster
❤RieBi&❤
❤RieBi&❤3mo ago
Your code is not thread safe It's very dangerous Look into that
SWEETPONY
SWEETPONYOP3mo ago
hm ur right, I will change List to smth concurrent like ConcurrentBag and try to see results forgot about thread safety simple for was x2 faster than Parallel.For idk why
❤RieBi&❤
❤RieBi&❤3mo ago
With concurrent bag and same code?
SWEETPONY
SWEETPONYOP3mo ago
yes
❤RieBi&❤
❤RieBi&❤3mo ago
Nothing unexpected tbf
blueberriesiftheywerecats
I think it would work if you provide needed capacity when creating list
Anton
Anton3mo ago
parallel has an overload with indices iirc, use that and an array
❤RieBi&❤
❤RieBi&❤3mo ago
Yeah so when multiple threads call the Add() at the same time, it would be essentially the same as doing it without parallel, but with parallel you also get overhead of syncing threads. So I decided to do it myself:
using System.Diagnostics;


var max = 100_000_000;
int[] array = Enumerable.Range(0, max).ToArray();
var lst = new List<double>();

var arr = new double[max];

var stopWatch = new Stopwatch();
stopWatch.Start();

for (int i = 0; i < array.Length; i++)
{
var result = Math.Pow(i, 100000000000);
arr[i] = result;
}

stopWatch.Stop();
Console.WriteLine(stopWatch.Elapsed);

var arr2 = new double[max];
stopWatch.Restart();

Parallel.For(0, array.Length, i =>
{
var result = Math.Pow(i, 100000000000);
arr2[i] = result;
});

stopWatch.Stop();
Console.WriteLine(stopWatch.Elapsed);
using System.Diagnostics;


var max = 100_000_000;
int[] array = Enumerable.Range(0, max).ToArray();
var lst = new List<double>();

var arr = new double[max];

var stopWatch = new Stopwatch();
stopWatch.Start();

for (int i = 0; i < array.Length; i++)
{
var result = Math.Pow(i, 100000000000);
arr[i] = result;
}

stopWatch.Stop();
Console.WriteLine(stopWatch.Elapsed);

var arr2 = new double[max];
stopWatch.Restart();

Parallel.For(0, array.Length, i =>
{
var result = Math.Pow(i, 100000000000);
arr2[i] = result;
});

stopWatch.Stop();
Console.WriteLine(stopWatch.Elapsed);
The second one runs almost 2x as fast as the former one. But I've got a shitty machine, so it might be even faster on your one. The key here is that every thread operates on their own indices, and yep it's faster
SWEETPONY
SWEETPONYOP3mo ago
oh, thanks for this!

Did you find this page helpful?