Skip to content

Instantly share code, notes, and snippets.

@roberocity
Created March 7, 2023 02:02
Show Gist options
  • Save roberocity/464b01a905654b3510ae544a06c24c3d to your computer and use it in GitHub Desktop.
Save roberocity/464b01a905654b3510ae544a06c24c3d to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 13,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [],
"source": [
"using static System.Console;\n",
"\n",
"class ShapeBase {\n",
" public ShapeBase() {}\n",
" public virtual double Area() => 0;\n",
" public virtual int Corners() => 0;\n",
"}\n",
"\n",
"class Circle : ShapeBase {\n",
" public Circle(double radius) => Radius = radius;\n",
" public double Radius { get; set; }\n",
" public override double Area() => Math.PI * Radius * Radius;\n",
" public override int Corners() => 0;\n",
"}\n",
"\n",
"class Square : ShapeBase {\n",
" public Square(double side) => Side = side;\n",
" public double Side { get; set; }\n",
" public override double Area() => Side * Side;\n",
" public override int Corners() => 4;\n",
"}\n",
"\n",
"class Rectangle : ShapeBase {\n",
" public Rectangle(double width, double height) {\n",
" Width = width;\n",
" Height = height;\n",
" }\n",
" public double Width { get; set; }\n",
" public double Height { get; set; }\n",
" public override double Area() => Width * Height;\n",
" public override int Corners() => 4;\n",
"}\n",
"\n",
"class Triangle : ShapeBase {\n",
" public Triangle(double width, double height) {\n",
" Width = width;\n",
" Height = height;\n",
" }\n",
" public double Width { get; set; }\n",
" public double Height { get; set; }\n",
" public override double Area() => Width * Height * 0.5;\n",
" public override int Corners() => 3;\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [],
"source": [
"#load \"TimeIt.cs\"\n",
"\n",
"double TotalArea(ShapeBase[] shapes) {\n",
" double total = 0;\n",
" foreach (var shape in shapes) {\n",
" total += shape.Area();\n",
" }\n",
" return total;\n",
"}\n",
"\n",
"double TotalAreaCornerWeighted(ShapeBase[] shapes) {\n",
" double total = 0;\n",
" foreach (var shape in shapes) {\n",
" total += shape.Area() * (1.0 / 1.0 + shape.Corners());\n",
" }\n",
" return total;\n",
"}\n",
"\n",
"var shapes = new ShapeBase[] {\n",
" new Circle(2),\n",
" new Square(3),\n",
" new Rectangle(2, 3),\n",
" new Triangle(3, 4),\n",
" new Circle(2),\n",
" new Square(3),\n",
" new Rectangle(2, 3),\n",
" new Triangle(3, 4),\n",
" new Circle(2),\n",
" new Square(3),\n",
" new Rectangle(2, 3),\n",
" new Triangle(3, 4),\n",
"};"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [
{
"data": {
"text/html": [
"<details open=\"open\" class=\"dni-treeview\"><summary><span class=\"dni-code-hint\"><code>TimedResult { MethodName = SOLID, ExecutionCount = 10039825 }</code></span></summary><div><table><thead><tr></tr></thead><tbody><tr><td>MethodName</td><td>SOLID</td></tr><tr><td>ExecutionCount</td><td><div class=\"dni-plaintext\"><pre>10039825</pre></div></td></tr></tbody></table></div></details><style>\r\n",
".dni-code-hint {\r\n",
" font-style: italic;\r\n",
" overflow: hidden;\r\n",
" white-space: nowrap;\r\n",
"}\r\n",
".dni-treeview {\r\n",
" white-space: nowrap;\r\n",
"}\r\n",
".dni-treeview td {\r\n",
" vertical-align: top;\r\n",
" text-align: start;\r\n",
"}\r\n",
"details.dni-treeview {\r\n",
" padding-left: 1em;\r\n",
"}\r\n",
"table td {\r\n",
" text-align: start;\r\n",
"}\r\n",
"table tr { \r\n",
" vertical-align: top; \r\n",
" margin: 0em 0px;\r\n",
"}\r\n",
"table tr td pre \r\n",
"{ \r\n",
" vertical-align: top !important; \r\n",
" margin: 0em 0px !important;\r\n",
"} \r\n",
"table th {\r\n",
" text-align: start;\r\n",
"}\r\n",
"</style>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"TimeIt.Time(2, \"SOLID\", () => TotalArea(shapes))"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {
"dotnet_interactive": {
"language": "csharp"
},
"polyglot_notebook": {
"kernelName": "csharp"
}
},
"outputs": [
{
"data": {
"text/html": [
"<details open=\"open\" class=\"dni-treeview\"><summary><span class=\"dni-code-hint\"><code>TimedResult { MethodName = SOLID , ExecutionCount = 7911264 }</code></span></summary><div><table><thead><tr></tr></thead><tbody><tr><td>MethodName</td><td>SOLID </td></tr><tr><td>ExecutionCount</td><td><div class=\"dni-plaintext\"><pre>7911264</pre></div></td></tr></tbody></table></div></details><style>\r\n",
".dni-code-hint {\r\n",
" font-style: italic;\r\n",
" overflow: hidden;\r\n",
" white-space: nowrap;\r\n",
"}\r\n",
".dni-treeview {\r\n",
" white-space: nowrap;\r\n",
"}\r\n",
".dni-treeview td {\r\n",
" vertical-align: top;\r\n",
" text-align: start;\r\n",
"}\r\n",
"details.dni-treeview {\r\n",
" padding-left: 1em;\r\n",
"}\r\n",
"table td {\r\n",
" text-align: start;\r\n",
"}\r\n",
"table tr { \r\n",
" vertical-align: top; \r\n",
" margin: 0em 0px;\r\n",
"}\r\n",
"table tr td pre \r\n",
"{ \r\n",
" vertical-align: top !important; \r\n",
" margin: 0em 0px !important;\r\n",
"} \r\n",
"table th {\r\n",
" text-align: start;\r\n",
"}\r\n",
"</style>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"TimeIt.Time(2, \"SOLID \", () => TotalAreaCornerWeighted(shapes))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".NET (C#)",
"language": "C#",
"name": ".net-csharp"
},
"polyglot_notebook": {
"kernelInfo": {
"defaultKernelName": "csharp",
"items": [
{
"aliases": [
"c#",
"C#"
],
"languageName": "C#",
"name": "csharp"
},
{
"aliases": [
"frontend"
],
"languageName": null,
"name": "vscode"
},
{
"aliases": [],
"languageName": "HTML",
"name": "html"
}
]
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment