| JS |
| var now = require("performance-now")
var padStart = require("pad-start")
function CreateId()
{
return padStart(Math.floor(Math.random() * 1e8).toString(), 10, '0');
}
class TestClass
{
constructor() {
this.Id = CreateId();
this.Value = CreateId();
}
}
for (var t = 0; t < 5; t++)
{
var startInit = now();
var vals = [];
for (var i = 0; i < 1000 * 1000; i++)
{
vals.push(new TestClass());
}
var endInit = now();
console.log("Init: " + (endInit - startInit) + " msecs");
}
|
| |
Init: 675.224758 msecs
Init: 580.368585 msecs
Init: 627.1724730000001 msecs
Init: 642.6952870000002 msecs
Init: 572.5845920000002 msecs
| C# |
| using System;
using System.Diagnostics;
namespace QuickSort
{
public class Program
{
public static void Main(string[] args)
{
var watch = new Stopwatch();
for (int i = 0; i < 5; i++)
{
watch.Restart();
var vals = new TestClass[1000 * 1000];
for (int j = 0; j < vals.Length; j++)
{
vals[j] = new TestClass();
}
watch.Stop();
Console.WriteLine($"Total: {watch.ElapsedMilliseconds} ms");
}
Console.WriteLine($"GC: {GC.CollectionCount(0)} {GC.CollectionCount(1)} {GC.CollectionCount(2)}");
Console.ReadLine();
}
}
internal class TestClass
{
public string Id = CreateGuid();
public string Value = CreateGuid();
private static readonly Random gen = new Random();
private static string CreateGuid() => gen.Next(100000000).ToString("0000000000");
}
}
|
| |
Total: 567 ms
Total: 652 ms
Total: 641 ms
Total: 606 ms
Total: 638 ms
GC: 114 60 15
Одинаково.
В C# в районе 40% времени приходится на стандартный рандом и 50% на ToString с параметром.