mirror of
https://github.com/vlang/v.git
synced 2025-09-08 06:41:58 -04:00
bench/vectors: add instructions on how to run the C# original code with dotnet run
, format vectors.cs with dotnet format
This commit is contained in:
parent
c44a89649a
commit
88cac9bdf4
@ -1,3 +1,22 @@
|
|||||||
|
Running the C# example:
|
||||||
|
```
|
||||||
|
dotnet run
|
||||||
|
```
|
||||||
|
|
||||||
|
Running the V program:
|
||||||
|
```
|
||||||
|
v crun vectors.v
|
||||||
|
```
|
||||||
|
|
||||||
|
Running the V program, compiled with -prod:
|
||||||
|
```
|
||||||
|
v -prod crun vectors.v
|
||||||
|
```
|
||||||
|
|
||||||
|
Note: the `crun` will make sure that the compilation will happen just
|
||||||
|
once at the start, and then the executable will be just reused by the
|
||||||
|
subsequent commands with identical options.
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
Benchmark 1: ./boids_test/bin/Release/net7.0/linux-x64/publish/boids_test
|
Benchmark 1: ./boids_test/bin/Release/net7.0/linux-x64/publish/boids_test
|
||||||
Time (mean ± σ): 262.2 ms ± 5.7 ms [User: 231.6 ms, System: 14.1 ms]
|
Time (mean ± σ): 262.2 ms ± 5.7 ms [User: 231.6 ms, System: 14.1 ms]
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
using System;
|
||||||
|
|
||||||
internal readonly struct Vector
|
internal readonly struct Vector
|
||||||
{
|
{
|
||||||
public readonly double X;
|
public readonly double X;
|
||||||
@ -10,26 +12,27 @@ internal readonly struct Vector
|
|||||||
Y = y;
|
Y = y;
|
||||||
Z = z;
|
Z = z;
|
||||||
}
|
}
|
||||||
|
|
||||||
public override string ToString()
|
public override string ToString()
|
||||||
{
|
{
|
||||||
return $"({X}, {Y}, {Z})";
|
return $"({X}, {Y}, {Z})";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static class Program
|
|
||||||
|
class Program
|
||||||
{
|
{
|
||||||
public static void Main()
|
static void Main(string[] args)
|
||||||
{
|
{
|
||||||
const int boidsCount = 10000;
|
const int boidsCount = 10000;
|
||||||
|
|
||||||
var positions = new Vector[boidsCount];
|
var positions = new Vector[boidsCount];
|
||||||
var velocities = new Vector[boidsCount];
|
var velocities = new Vector[boidsCount];
|
||||||
|
|
||||||
const double maxCoordinate = 10000.0;
|
const double maxCoordinate = 10000.0;
|
||||||
|
|
||||||
var random = new Random();
|
var random = new Random();
|
||||||
|
|
||||||
for (var positionIndex = 0; positionIndex < positions.Length; positionIndex++)
|
for (var positionIndex = 0; positionIndex < positions.Length; positionIndex++)
|
||||||
{
|
{
|
||||||
positions[positionIndex] = new Vector(
|
positions[positionIndex] = new Vector(
|
||||||
@ -38,30 +41,30 @@ internal static class Program
|
|||||||
z: random.NextDouble() * maxCoordinate
|
z: random.NextDouble() * maxCoordinate
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
const double cohesionDistance = 10.0;
|
const double cohesionDistance = 10.0;
|
||||||
const double separationDistance = 5.0;
|
const double separationDistance = 5.0;
|
||||||
|
|
||||||
var closeBoids = new List<int>();
|
var closeBoids = new List<int>();
|
||||||
|
|
||||||
for (var boidIndex = 0; boidIndex < positions.Length; boidIndex++)
|
for (var boidIndex = 0; boidIndex < positions.Length; boidIndex++)
|
||||||
{
|
{
|
||||||
var position = positions[boidIndex];
|
var position = positions[boidIndex];
|
||||||
closeBoids.Clear();
|
closeBoids.Clear();
|
||||||
|
|
||||||
for (var otherBoidIndex = 0; otherBoidIndex < positions.Length; otherBoidIndex++)
|
for (var otherBoidIndex = 0; otherBoidIndex < positions.Length; otherBoidIndex++)
|
||||||
{
|
{
|
||||||
if (boidIndex == otherBoidIndex)
|
if (boidIndex == otherBoidIndex)
|
||||||
{
|
{
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
var otherPosition = positions[otherBoidIndex];
|
var otherPosition = positions[otherBoidIndex];
|
||||||
|
|
||||||
var differenceX = position.X - otherPosition.X;
|
var differenceX = position.X - otherPosition.X;
|
||||||
var differenceY = position.Y - otherPosition.Y;
|
var differenceY = position.Y - otherPosition.Y;
|
||||||
var differenceZ = position.Z - otherPosition.Z;
|
var differenceZ = position.Z - otherPosition.Z;
|
||||||
|
|
||||||
var distance = differenceX * differenceX +
|
var distance = differenceX * differenceX +
|
||||||
differenceY * differenceY +
|
differenceY * differenceY +
|
||||||
differenceZ * differenceZ;
|
differenceZ * differenceZ;
|
||||||
@ -112,20 +115,20 @@ internal static class Program
|
|||||||
}
|
}
|
||||||
|
|
||||||
var closeBoidVelocity = velocities[closeBoidIndex];
|
var closeBoidVelocity = velocities[closeBoidIndex];
|
||||||
|
|
||||||
alignment = new Vector(
|
alignment = new Vector(
|
||||||
alignment.X + closeBoidVelocity.X,
|
alignment.X + closeBoidVelocity.X,
|
||||||
alignment.Y + closeBoidVelocity.Y,
|
alignment.Y + closeBoidVelocity.Y,
|
||||||
alignment.Z + closeBoidVelocity.Z
|
alignment.Z + closeBoidVelocity.Z
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
cohesion = new Vector(
|
cohesion = new Vector(
|
||||||
cohesion.X / closeBoids.Count,
|
cohesion.X / closeBoids.Count,
|
||||||
cohesion.Y / closeBoids.Count,
|
cohesion.Y / closeBoids.Count,
|
||||||
cohesion.Z / closeBoids.Count
|
cohesion.Z / closeBoids.Count
|
||||||
);
|
);
|
||||||
|
|
||||||
var cohesionForce = new Vector(
|
var cohesionForce = new Vector(
|
||||||
cohesion.X - position.X,
|
cohesion.X - position.X,
|
||||||
cohesion.Y - position.Y,
|
cohesion.Y - position.Y,
|
||||||
@ -140,7 +143,7 @@ internal static class Program
|
|||||||
separation.Z / separationCount
|
separation.Z / separationCount
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
alignment = new Vector(
|
alignment = new Vector(
|
||||||
alignment.X / closeBoids.Count,
|
alignment.X / closeBoids.Count,
|
||||||
alignment.Y / closeBoids.Count,
|
alignment.Y / closeBoids.Count,
|
||||||
@ -148,7 +151,7 @@ internal static class Program
|
|||||||
);
|
);
|
||||||
|
|
||||||
var currentVelocity = velocities[boidIndex];
|
var currentVelocity = velocities[boidIndex];
|
||||||
|
|
||||||
velocities[boidIndex] = new Vector(
|
velocities[boidIndex] = new Vector(
|
||||||
currentVelocity.X + cohesionForce.X + separation.X + alignment.X,
|
currentVelocity.X + cohesionForce.X + separation.X + alignment.X,
|
||||||
currentVelocity.Y + cohesionForce.Y + separation.Y + alignment.Y,
|
currentVelocity.Y + cohesionForce.Y + separation.Y + alignment.Y,
|
||||||
@ -158,32 +161,33 @@ internal static class Program
|
|||||||
|
|
||||||
var positionSum = new Vector(0.0, 0.0, 0.0);
|
var positionSum = new Vector(0.0, 0.0, 0.0);
|
||||||
var velocitySum = new Vector(0.0, 0.0, 0.0);
|
var velocitySum = new Vector(0.0, 0.0, 0.0);
|
||||||
|
|
||||||
for (var boidIndex = 0; boidIndex < positions.Length; boidIndex++)
|
for (var boidIndex = 0; boidIndex < positions.Length; boidIndex++)
|
||||||
{
|
{
|
||||||
var position = positions[boidIndex];
|
var position = positions[boidIndex];
|
||||||
var velocity = velocities[boidIndex];
|
var velocity = velocities[boidIndex];
|
||||||
|
|
||||||
positions[boidIndex] = new Vector(
|
positions[boidIndex] = new Vector(
|
||||||
position.X + velocity.X,
|
position.X + velocity.X,
|
||||||
position.Y + velocity.Y,
|
position.Y + velocity.Y,
|
||||||
position.Z + velocity.Z
|
position.Z + velocity.Z
|
||||||
);
|
);
|
||||||
|
|
||||||
positionSum = new Vector(
|
positionSum = new Vector(
|
||||||
positionSum.X + position.X,
|
positionSum.X + position.X,
|
||||||
positionSum.Y + position.Y,
|
positionSum.Y + position.Y,
|
||||||
positionSum.Z + position.Z
|
positionSum.Z + position.Z
|
||||||
);
|
);
|
||||||
|
|
||||||
velocitySum = new Vector(
|
velocitySum = new Vector(
|
||||||
velocitySum.X + velocity.X,
|
velocitySum.X + velocity.X,
|
||||||
velocitySum.Y + velocity.Y,
|
velocitySum.Y + velocity.Y,
|
||||||
velocitySum.Z + velocity.Z
|
velocitySum.Z + velocity.Z
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Console.WriteLine(positionSum.ToString());
|
Console.WriteLine(positionSum.ToString());
|
||||||
Console.WriteLine(velocitySum.ToString());
|
Console.WriteLine(velocitySum.ToString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
14
bench/vectors/vectors.csproj
Normal file
14
bench/vectors/vectors.csproj
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
<PublishSingleFile>true</PublishSingleFile>
|
||||||
|
<SelfContained>true</SelfContained>
|
||||||
|
|
||||||
|
<TargetFramework>net7.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
<OutputType>Exe</OutputType>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
</Project>
|
1
bench/vectors/vectors.runtimeconfig.json
Normal file
1
bench/vectors/vectors.runtimeconfig.json
Normal file
@ -0,0 +1 @@
|
|||||||
|
{"runtimeOptions":{"framework":{"name":"Microsoft.NETCore.App","version":"7.0.10"}}}
|
Loading…
x
Reference in New Issue
Block a user