CARVIEW |
Select Language
HTTP/2 200
date: Sun, 27 Jul 2025 17:35:22 GMT
content-type: text/html; charset=utf-8
vary: X-PJAX, X-PJAX-Container, Turbo-Visit, Turbo-Frame, X-Requested-With,Accept-Encoding, Accept, X-Requested-With
etag: W/"eed8f3c5402e160034669fade9afd7be"
cache-control: max-age=0, private, must-revalidate
strict-transport-security: max-age=31536000; includeSubdomains; preload
x-frame-options: deny
x-content-type-options: nosniff
x-xss-protection: 0
referrer-policy: origin-when-cross-origin, strict-origin-when-cross-origin
content-security-policy: default-src 'none'; base-uri 'self'; child-src github.githubassets.com github.com/assets-cdn/worker/ github.com/assets/ gist.github.com/assets-cdn/worker/; connect-src 'self' uploads.github.com www.githubstatus.com collector.github.com raw.githubusercontent.com api.github.com github-cloud.s3.amazonaws.com github-production-repository-file-5c1aeb.s3.amazonaws.com github-production-upload-manifest-file-7fdce7.s3.amazonaws.com github-production-user-asset-6210df.s3.amazonaws.com *.rel.tunnels.api.visualstudio.com wss://*.rel.tunnels.api.visualstudio.com objects-origin.githubusercontent.com copilot-proxy.githubusercontent.com proxy.individual.githubcopilot.com proxy.business.githubcopilot.com proxy.enterprise.githubcopilot.com *.actions.githubusercontent.com wss://*.actions.githubusercontent.com productionresultssa0.blob.core.windows.net/ productionresultssa1.blob.core.windows.net/ productionresultssa2.blob.core.windows.net/ productionresultssa3.blob.core.windows.net/ productionresultssa4.blob.core.windows.net/ productionresultssa5.blob.core.windows.net/ productionresultssa6.blob.core.windows.net/ productionresultssa7.blob.core.windows.net/ productionresultssa8.blob.core.windows.net/ productionresultssa9.blob.core.windows.net/ productionresultssa10.blob.core.windows.net/ productionresultssa11.blob.core.windows.net/ productionresultssa12.blob.core.windows.net/ productionresultssa13.blob.core.windows.net/ productionresultssa14.blob.core.windows.net/ productionresultssa15.blob.core.windows.net/ productionresultssa16.blob.core.windows.net/ productionresultssa17.blob.core.windows.net/ productionresultssa18.blob.core.windows.net/ productionresultssa19.blob.core.windows.net/ github-production-repository-image-32fea6.s3.amazonaws.com github-production-release-asset-2e65be.s3.amazonaws.com insights.github.com wss://alive.github.com api.githubcopilot.com api.individual.githubcopilot.com api.business.githubcopilot.com api.enterprise.githubcopilot.com; font-src github.githubassets.com; form-action 'self' github.com gist.github.com copilot-workspace.githubnext.com objects-origin.githubusercontent.com; frame-ancestors 'none'; frame-src viewscreen.githubusercontent.com notebooks.githubusercontent.com; img-src 'self' data: blob: github.githubassets.com media.githubusercontent.com camo.githubusercontent.com identicons.github.com avatars.githubusercontent.com private-avatars.githubusercontent.com github-cloud.s3.amazonaws.com objects.githubusercontent.com release-assets.githubusercontent.com secured-user-images.githubusercontent.com/ user-images.githubusercontent.com/ private-user-images.githubusercontent.com opengraph.githubassets.com copilotprodattachments.blob.core.windows.net/github-production-copilot-attachments/ github-production-user-asset-6210df.s3.amazonaws.com customer-stories-feed.github.com spotlights-feed.github.com objects-origin.githubusercontent.com *.githubusercontent.com; manifest-src 'self'; media-src github.com user-images.githubusercontent.com/ secured-user-images.githubusercontent.com/ private-user-images.githubusercontent.com github-production-user-asset-6210df.s3.amazonaws.com gist.github.com; script-src github.githubassets.com; style-src 'unsafe-inline' github.githubassets.com; upgrade-insecure-requests; worker-src github.githubassets.com github.com/assets-cdn/worker/ github.com/assets/ gist.github.com/assets-cdn/worker/
server: github.com
content-encoding: gzip
accept-ranges: bytes
set-cookie: _gh_sess=wNRrqZ%2FHKb%2B%2FkCU%2FgX5txW2p4i0AhAG%2BR98qHALsOVA3UUUw8nDkILxYtdcNUoD519iybtXMLABmbCPI7qzIS8%2FoI1Md%2B%2F7PuMJSMaMh%2BiJWdPJRZ0N0J3S1XxSX8AlqbRz6DN3kDIRGtsxrVRus1gh8QaWU1zx6OwBmoIVxoftCgGwVOARa4ImHZvwg2SmVRLlvIGDphwAa%2Fi7Os5eKfu2%2BN9kUUbCTkaHY95mepZ6V%2FTLpxmTr7etvPFNB%2BTI5NtJkGlEDxRbz8qNrIPtnWA%3D%3D--%2FpYzL4zyCyF2CTpa--FQham2uBInLutKaTeXS5%2FQ%3D%3D; Path=/; HttpOnly; Secure; SameSite=Lax
set-cookie: _octo=GH1.1.1123663664.1753637722; Path=/; Domain=github.com; Expires=Mon, 27 Jul 2026 17:35:22 GMT; Secure; SameSite=Lax
set-cookie: logged_in=no; Path=/; Domain=github.com; Expires=Mon, 27 Jul 2026 17:35:22 GMT; HttpOnly; Secure; SameSite=Lax
x-github-request-id: EC42:3240E6:1064194:157CEC7:6886635A
A benchmark code for struct generics · GitHub
Show Gist options
Save ufcpp/15af6d3d7606fb3771a91c81898dcfa3 to your computer and use it in GitHub Desktop.
{{ message }}
Instantly share code, notes, and snippets.
Created
February 22, 2017 17:34
-
Star
1
(1)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save ufcpp/15af6d3d7606fb3771a91c81898dcfa3 to your computer and use it in GitHub Desktop.
A benchmark code for struct generics
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using BenchmarkDotNet.Attributes; | |
using BenchmarkDotNet.Attributes.Jobs; | |
using BenchmarkDotNet.Engines; | |
using System; | |
public interface IBinaryOperator | |
{ | |
int Operate(int x, int y); | |
} | |
public struct Add : IBinaryOperator | |
{ | |
public int Operate(int x, int y) => x + y; | |
} | |
public struct Mul : IBinaryOperator | |
{ | |
public int Operate(int x, int y) => x * y; | |
} | |
public static class Op | |
{ | |
public static int Add(int x, int y) => x + y; | |
public static int Mul(int x, int y) => x * y; | |
} | |
class Program | |
{ | |
static void Main() | |
{ | |
BenchmarkDotNet.Running.BenchmarkRunner.Run<TestBenchmark>(); | |
} | |
} | |
[SimpleJob(RunStrategy.Throughput)] | |
public class TestBenchmark | |
{ | |
static (int x, int y)[] _data; | |
static TestBenchmark() | |
{ | |
var r = new Random(); | |
_data = new(int x, int y)[10]; | |
for (int i = 0; i < _data.Length; i++) | |
{ | |
_data[i] = (r.Next(), r.Next()); | |
} | |
} | |
[Benchmark] public static void EmbeddedAdd() { foreach (var (x, y) in _data) Embedded.Add(x, y); } | |
[Benchmark] public static void StaticAdd() { foreach (var (x, y) in _data) StaticMethod.Add(x, y); } | |
[Benchmark] public static void DefaultAdd() { foreach (var (x, y) in _data) DefaultValue.Add(x, y); } | |
[Benchmark] public static void TypeClassAdd() { foreach (var (x, y) in _data) TypeClass.Op<Add>(x, y); } | |
[Benchmark] public static void DelegateAdd() { foreach (var (x, y) in _data) DelegateParameter.Op(x, y, Op.Add); } | |
[Benchmark] public static void CachedDelegateAdd() { Func<int, int, int> f = Op.Add; foreach (var (x, y) in _data) DelegateParameter.Op(x, y, f); } | |
[Benchmark] public static void InterfaceAdd() { foreach (var (x, y) in _data) InterfaceParameter.Op(x, y, new Add()); } | |
[Benchmark] public static void CachedInterfaceAdd() { IBinaryOperator op = new Add(); foreach (var (x, y) in _data) InterfaceParameter.Op(x, y, op); } | |
[Benchmark] public static void EmbeddedMul() { foreach (var (x, y) in _data) Embedded.Mul(x, y); } | |
[Benchmark] public static void StaticMul() { foreach (var (x, y) in _data) StaticMethod.Mul(x, y); } | |
[Benchmark] public static void DefaultMul() { foreach (var (x, y) in _data) DefaultValue.Mul(x, y); } | |
[Benchmark] public static void TypeClassMul() { foreach (var (x, y) in _data) TypeClass.Op<Mul>(x, y); } | |
[Benchmark] public static void DelegateMul() { foreach (var (x, y) in _data) DelegateParameter.Op(x, y, Op.Mul); } | |
[Benchmark] public static void CachedDelegateMul() { Func<int, int, int> f = Op.Mul; foreach (var (x, y) in _data) DelegateParameter.Op(x, y, f); } | |
[Benchmark] public static void InterfaceMul() { foreach (var (x, y) in _data) InterfaceParameter.Op(x, y, new Mul()); } | |
[Benchmark] public static void CachedInterfaceMul() { IBinaryOperator op = new Mul(); foreach (var (x, y) in _data) InterfaceParameter.Op(x, y, op); } | |
} | |
public class Embedded | |
{ | |
public static int Add(int x, int y) | |
{ | |
return x + y; | |
} | |
public static int Mul(int x, int y) | |
{ | |
return x * y; | |
} | |
} | |
public class StaticMethod | |
{ | |
public static int Add(int x, int y) | |
{ | |
return Op.Add(x, y); | |
} | |
public static int Mul(int x, int y) | |
{ | |
return Op.Mul(x, y); | |
} | |
} | |
public class DefaultValue | |
{ | |
public static int Add(int x, int y) | |
{ | |
return default(Add).Operate(x, y); | |
} | |
public static int Mul(int x, int y) | |
{ | |
return default(Mul).Operate(x, y); | |
} | |
} | |
public class TypeClass | |
{ | |
public static int Op<T>(int x, int y) | |
where T : struct, IBinaryOperator | |
{ | |
return default(T).Operate(x, y); | |
} | |
} | |
public class DelegateParameter | |
{ | |
public static int Op(int x, int y, Func<int, int, int> f) | |
{ | |
return f(x, y); | |
} | |
} | |
public class InterfaceParameter | |
{ | |
public static int Op(int x, int y, IBinaryOperator op) | |
{ | |
return op.Operate(x, y); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can’t perform that action at this time.