Skip to content

Instantly share code, notes, and snippets.

@HumanEquivalentUnit
Created December 15, 2017 21:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HumanEquivalentUnit/183bfd109bb28cce644b3f60ca83d81f to your computer and use it in GitHub Desktop.
Save HumanEquivalentUnit/183bfd109bb28cce644b3f60ca83d81f to your computer and use it in GitHub Desktop.
adventofcode powershell vs c# timings
# C# runs in ~0.5 seconds.
# powershell commented out below runs in ~23 seconds.
# correct answer is 567
Add-Type @'
using System;
public class MyType1
{
public long crunch() {
long gA = 512;
long gB = 191;
long bitmask = 65535;
long numMatches = 0;
for (long i=0; i < 40000000; i++)
{
gA = (gA * 16807) % 2147483647;
gB = (gB * 48271) % 2147483647;
if ( (gA & bitmask) == (gB & bitmask) ) { numMatches++; }
}
return numMatches;
}
}
'@ -Language CSharp
$obj1 = New-Object MyType1
$obj1.crunch()
#[uint64]$gA = 512
#[uint64]$gB = 191
#
#[uint16]$bitmask = 65535
#[uint64]$numMatches = 0
#for ([uint64]$i=0; $i -lt 40000000; $i++)
#{
#
# $gA = ($gA * 16807) % 2147483647
# $gB = ($gB * 48271) % 2147483647
#
# if ( ($gA -band $bitmask) -eq ($gB -band $bitmask) ) { $numMatches++ }
#}
#
#$numMatches
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment