Skip to content

Instantly share code, notes, and snippets.

@thomaslevesque
thomaslevesque / memset.cs
Last active July 6, 2021 01:48
Benchmark of memset implementations in C# using a loop or the initblk CLR instruction (run in LinqPad)
void Main()
{
var array = new byte[10000000];
var initBlk = new InitblkMemoryHelper();
var loop = new LoopMemoryHelper();
// First run for JIT warmup and type initialization
initBlk.Memset(array, 0, array.Length, 42);
loop.Memset(array, 0, array.Length, 42);
@apangin
apangin / HotSpot JVM intrinsics
Last active May 11, 2023 18:32
HotSpot JVM intrinsics
_hashCode java/lang/Object.hashCode()I
_getClass java/lang/Object.getClass()Ljava/lang/Class;
_clone java/lang/Object.clone()Ljava/lang/Object;
_dabs java/lang/Math.abs(D)D
_dsin java/lang/Math.sin(D)D
_dcos java/lang/Math.cos(D)D
_dtan java/lang/Math.tan(D)D
_datan2 java/lang/Math.atan2(DD)D
_dsqrt java/lang/Math.sqrt(D)D
_dlog java/lang/Math.log(D)D
@bretcope
bretcope / 1 - Map Arguments to Properties.cs
Last active March 12, 2019 05:33
Sigil Object Mapping - Basic Examples
// Runnable from Linqpad 5
// Must install the Sigil nuget package and include the Sigil namespace
void Main()
{
var createSample = GetMapperDelegate();
// try it out
createSample(23, "Hello").Dump();
}
public static bool ContainsTokenUnroll(string value, string token, char delimiter = ';')
{
if (string.IsNullOrEmpty(token)) return false;
if (string.IsNullOrEmpty(value)) return false;
var valueLength = value.Length;
var tokenLength = token.Length;
if (tokenLength > valueLength) return false;
@xoofx
xoofx / EvilArray.cs
Created June 1, 2016 21:56
Cast an array of blittable structs to an array of byte[], transform length as well
using System;
using System.Runtime.InteropServices;
namespace EvilArray
{
/// <summary>
/// Cast an array of structs to an array of byte[]
/// </summary>
class Program
{
@benaadams
benaadams / FastIntegerToIPAddress.cs
Last active June 9, 2016 10:01
Fast unit ipaddress to string
public unsafe static string FastIntegerToIPAddress(uint input)
{
var input0 = (input & 0xff000000) >> 24;
var input1 = (input & 0xff0000) >> 16;
var input2 = (input & 0xff00) >> 8;
var input3 = (input & 0xff);
var length0 = input0 > 99 ? 3 : input0 > 9 ? 2 : 1;
var length1 = input1 > 99 ? 3 : input1 > 9 ? 2 : 1;
var length2 = input2 > 99 ? 3 : input2 > 9 ? 2 : 1;
using Microsoft.Diagnostics.Tracing;
using Microsoft.Diagnostics.Tracing.Parsers;
using Microsoft.Diagnostics.Tracing.Parsers.Clr;
using Microsoft.Diagnostics.Tracing.Session;
using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
@NickCraver
NickCraver / ExampleUsage.cs
Last active November 27, 2021 04:24
Code to mark a SQL string before it's passed to Dapper.
public static List<T> Query<T>(this DataContext db, string sql, object param = null, int? commandTimeout = null, IDbTransaction transaction = null, [CallerFilePath]string fromFile = null, [CallerLineNumber]int onLine = 0, string comment = null)
{
using (db.Connection.EnsureOpen())
{
try
{
return db.Connection.Query<T>(MarkSqlString(sql, fromFile, onLine, comment), param, transaction ?? db.Transaction, true, commandTimeout).AsDapperList();
}
catch (SqlException ex) when (ex.Is(SqlErrorCode.DatabaseReadOnly_3906))
{
@aL3891
aL3891 / dateformatbenchmark.cs
Last active October 6, 2016 08:48
dateformat
using System;
using static System.Text.Encoding;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Diagnostics.Windows;
using BenchmarkDotNet.Running;
using Xunit;
using System.Globalization;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;