From 3eab373226f3a1055a1466c421008d00671ec270 Mon Sep 17 00:00:00 2001
From: Tian jianyong <11429339@qq.com>
Date: Fri, 6 Jun 2025 17:22:18 +0800
Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=BA=A2=E5=A4=96=E5=9B=BE?=
=?UTF-8?q?=E5=83=8F=E6=80=A7=E8=83=BD=EF=BC=8C=E6=94=B9=E5=9B=9Edouble?=
=?UTF-8?q?=E5=AD=98=E5=82=A8?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../SimulationManagerPerformanceTest.cs | 4 +-
ThreatSource/src/Guidance/InfraredImage.cs | 150 ++----------------
docs/project/test_method.md | 7 +-
.../performance_test_result_itg_0605.md | 47 +++++-
4 files changed, 68 insertions(+), 140 deletions(-)
diff --git a/ThreatSource.Tests/src/Simulation/SimulationManagerPerformanceTest.cs b/ThreatSource.Tests/src/Simulation/SimulationManagerPerformanceTest.cs
index ee21e2d..f71ca48 100644
--- a/ThreatSource.Tests/src/Simulation/SimulationManagerPerformanceTest.cs
+++ b/ThreatSource.Tests/src/Simulation/SimulationManagerPerformanceTest.cs
@@ -170,8 +170,8 @@ namespace ThreatSource.Tests.Simulation
//string[] missileTypes = ["lsgm_001"];
//string[] missileTypes = ["lbr_001"];
//string[] missileTypes = ["irc_001"];
- //string[] missileTypes = ["itg_001"];
- string[] missileTypes = ["mmw_001"];
+ string[] missileTypes = ["itg_001"];
+ //string[] missileTypes = ["mmw_001"];
//string[] missileTypes = ["tsm_001"];
string missileType = missileTypes[Random.Shared.Next(missileTypes.Length)];
diff --git a/ThreatSource/src/Guidance/InfraredImage.cs b/ThreatSource/src/Guidance/InfraredImage.cs
index cca7397..55535b1 100644
--- a/ThreatSource/src/Guidance/InfraredImage.cs
+++ b/ThreatSource/src/Guidance/InfraredImage.cs
@@ -9,7 +9,7 @@ namespace ThreatSource.Guidance
/// 该类存储红外图像的基本信息:
/// - 图像尺寸
/// - 像素物理大小
- /// - 红外强度矩阵(使用byte存储,对数缩放)
+ /// - 红外强度矩阵
/// 用于目标探测和识别
///
///
@@ -17,79 +17,6 @@ namespace ThreatSource.Guidance
///
public class InfraredImage
{
- // === 对数缩放参数 ===
- ///
- /// 强度值的最小值,单位:W/sr
- /// 默认值1e-6 W/sr,对应约273K(0°C)的物体在合理距离下的辐射强度
- ///
- private const double MIN_INTENSITY = 1e-6;
-
- ///
- /// 强度值的最大值,单位:W/sr
- /// 默认值1e3 W/sr 对应高温物体在近距离的辐射强度上限
- ///
- private const double MAX_INTENSITY = 1e3;
-
- ///
- /// 最小强度值的对数(log10)
- ///
- private static readonly double MIN_LOG = Math.Log10(MIN_INTENSITY);
-
- ///
- /// 最大强度值的对数(log10)
- ///
- private static readonly double MAX_LOG = Math.Log10(MAX_INTENSITY);
-
- ///
- /// 对数范围
- ///
- private static readonly double LOG_RANGE = MAX_LOG - MIN_LOG;
-
- ///
- /// 预计算的强度值查找表,避免重复的对数运算
- /// 索引0-255对应byte值,值为对应的强度值(W/sr)
- ///
- private static readonly double[] INTENSITY_LOOKUP_TABLE = new double[256];
-
- ///
- /// 反向查找表:强度值到byte的快速映射
- /// 使用分段线性插值优化SetIntensity性能
- ///
- private static readonly double[] SORTED_INTENSITY_VALUES = new double[256];
-
-
-
- ///
- /// 静态构造函数,初始化查找表
- ///
- static InfraredImage()
- {
- // 预计算所有可能的byte值对应的强度值
- for (int i = 0; i < 256; i++)
- {
- INTENSITY_LOOKUP_TABLE[i] = ComputeIntensityFromByte((byte)i);
- SORTED_INTENSITY_VALUES[i] = INTENSITY_LOOKUP_TABLE[i];
- }
- }
-
- ///
- /// 计算byte值对应的强度值(仅在静态构造函数中使用)
- ///
- /// byte值
- /// 强度值,单位:W/sr
- private static double ComputeIntensityFromByte(byte byteValue)
- {
- // 处理零值
- if (byteValue == 0)
- return 0.0;
-
- // 反向对数缩放
- double normalizedLog = (byteValue - 1) / 254.0;
- double logIntensity = MIN_LOG + normalizedLog * LOG_RANGE;
- return Math.Pow(10, logIntensity);
- }
-
- // === 基本属性 ===
///
/// 图像宽度,单位:像素
///
@@ -106,10 +33,10 @@ namespace ThreatSource.Guidance
public double PixelSize { get; private set; }
///
- /// 红外强度矩阵,使用byte存储(对数缩放)
- /// 0 = 零强度,1-255 = 对数缩放的强度值
+ /// 红外强度矩阵
+ /// 单位:W/sr
///
- private byte[,] intensityData;
+ private double[,] intensityData;
///
/// 图像中心视线方向
@@ -135,7 +62,7 @@ namespace ThreatSource.Guidance
Height = height;
PixelSize = pixelSize;
LineOfSight = lineOfSight;
- intensityData = new byte[height, width];
+ intensityData = new double[height, width];
SmokeCoverageMask = smokeCoverageMask ?? new bool[height, width];
}
@@ -158,7 +85,7 @@ namespace ThreatSource.Guidance
// 重新分配强度数据数组(如果尺寸不匹配)
if (intensityData == null || intensityData.GetLength(0) != height || intensityData.GetLength(1) != width)
{
- intensityData = new byte[height, width];
+ intensityData = new double[height, width];
}
else
{
@@ -177,12 +104,12 @@ namespace ThreatSource.Guidance
{
if (row >= 0 && row < Height && col >= 0 && col < Width)
{
- intensityData[row, col] = ConvertToByte(value);
+ intensityData[row, col] = value;
}
}
///
- /// 获取像素强度值(使用预计算查找表,高性能)
+ /// 获取像素强度值
///
/// 行索引
/// 列索引
@@ -191,74 +118,29 @@ namespace ThreatSource.Guidance
{
if (row >= 0 && row < Height && col >= 0 && col < Width)
{
- return INTENSITY_LOOKUP_TABLE[intensityData[row, col]];
+ return intensityData[row, col];
}
return 0.0;
}
- ///
- /// 将强度值转换为byte(简化版本,高性能)
- ///
- /// 强度值,单位:W/sr
- /// 缩放后的byte值
- private static byte ConvertToByte(double intensity)
- {
- // 处理零值和负值
- if (intensity <= 0)
- return 0;
-
- // 处理超出范围的值
- if (intensity <= SORTED_INTENSITY_VALUES[1])
- return 1;
- if (intensity >= SORTED_INTENSITY_VALUES[255])
- return 255;
-
- // 使用简化的二分查找
- return SimpleBinarySearch(intensity);
- }
-
- ///
- /// 简化的二分查找,直接返回最接近的索引(移除距离计算)
- ///
- private static byte SimpleBinarySearch(double intensity)
- {
- int left = 1;
- int right = 255;
-
- // 标准二分查找
- while (left < right)
- {
- int mid = (left + right) >> 1; // 使用位移代替除法
- if (SORTED_INTENSITY_VALUES[mid] < intensity)
- left = mid + 1;
- else
- right = mid;
- }
-
- return (byte)left;
- }
-
///
/// 获取内存占用信息(用于性能监控)
///
/// 内存占用字节数
public long GetMemoryUsage()
{
- return sizeof(byte) * Width * Height; // byte数组
+ return sizeof(double) * Width * Height; // double数组
}
///
- /// 获取缩放参数信息(用于调试)
+ /// 获取图像信息(用于调试)
///
- /// 缩放参数的调试信息
- public string GetScalingInfo()
+ /// 图像的调试信息
+ public string GetImageInfo()
{
- return $"强度范围: [{MIN_INTENSITY:E3}, {MAX_INTENSITY:E3}] W/sr, " +
- $"对数范围: [{MIN_LOG:F1}, {MAX_LOG:F1}], " +
- $"相对精度: ~4.3%, " +
- $"内存占用: {GetMemoryUsage():N0} bytes, " +
- $"查找表: {INTENSITY_LOOKUP_TABLE.Length * sizeof(double):N0} bytes, " +
- $"算法: 简化二分查找";
+ return $"图像尺寸: {Width}x{Height}, " +
+ $"像素大小: {PixelSize:E3} 弧度/像素, " +
+ $"内存占用: {GetMemoryUsage():N0} bytes";
}
}
}
\ No newline at end of file
diff --git a/docs/project/test_method.md b/docs/project/test_method.md
index 3f3f6c1..6f42218 100644
--- a/docs/project/test_method.md
+++ b/docs/project/test_method.md
@@ -32,7 +32,7 @@ dotnet test --filter "Category!=Performance"
6. 仅运行性能测试:
```bash
-dotnet test --filter "Category=Performance"
+dotnet test --filter "Category=Performance" --logger "console;verbosity=detailed"
```
7. 按类名排除:
@@ -45,6 +45,11 @@ dotnet test --filter "FullyQualifiedName!~PerformanceTest"
dotnet test --filter "Category!=Performance&FullyQualifiedName!~Performance"
```
+9. 运行 Release 模式下的性能测试:
+```bash
+dotnet test --configuration Release --filter "RunPerformanceTest" --verbosity minimal --nologo --logger "console;verbosity=normal"
+```
+
### 测试报告
时间:2025-05-16
测试目的:测试 Vector3D 作为类和结构体的性能差异
diff --git a/docs/test_result/performance_test_result_itg_0605.md b/docs/test_result/performance_test_result_itg_0605.md
index 9f4180e..1cc7ee5 100644
--- a/docs/test_result/performance_test_result_itg_0605.md
+++ b/docs/test_result/performance_test_result_itg_0605.md
@@ -200,7 +200,7 @@ dotnet test --configuration Release --filter "RunPerformanceTest" --verbosity mi
- 优化效果:
- GC次数减少 60%,效果不错,但帧时间有所上升
-- 是否采用:是
+- 是否采用:否,考虑提高了程序复杂度,提高了帧时间
=== 详细性能测试结果 ===
@@ -284,7 +284,7 @@ dotnet test --configuration Release --filter "RunPerformanceTest" --verbosity mi
- 优化效果:
- 没有什么变化
-- 是否采用:是,考虑将Intensity从对数运算改为查找表
+- 是否采用:否,考虑提高了程序复杂度,提高了帧时间
=== 详细性能测试结果 ===
@@ -407,4 +407,45 @@ Gen2 GC次数: 0 (0.0 次/秒)
帧时间表现: 优秀 ✅
内存管理: 优秀 ✅
GC频率: 优秀 ✅
-【潜在问题分析】
\ No newline at end of file
+【潜在问题分析】
+
+ ### 第八次优化
+
+时间:2025-06-05 12:00:00
+
+- 优化内容:
+ - 将 InfraredImage 的 Intensity 数组从 byte 改回 double,去掉对数计算和查找表
+
+- 优化效果:
+ - 帧时间减小,GC次数增加,但影响不大
+
+- 是否采用:是
+
+=== 详细性能测试结果 ===
+【基本统计】
+测试时长: 30.0 秒
+总更新次数: 1416
+平均FPS: 47.2
+平均帧时间: 1.10 ms
+【帧时间分析】
+最小帧时间: 0.00 ms
+最大帧时间: 19.10 ms
+95%分位数: 5.03 ms
+99%分位数: 7.89 ms
+【内存使用分析】
+起始内存: 2.93 MB
+结束内存: 8.54 MB
+峰值内存: 9.52 MB
+内存增长: 5.61 MB
+平均内存增长率: 0.19 MB/s
+【垃圾回收分析】
+Gen0 GC次数: 93 (3.1 次/秒)
+Gen1 GC次数: 79 (2.6 次/秒)
+Gen2 GC次数: 69 (2.3 次/秒)
+总GC次数: 241
+【性能评级】
+帧时间表现: 优秀 ✅
+内存管理: 优秀 ✅
+GC频率: 一般 ⚠️
+【潜在问题分析】
+⚠️ Gen0 GC频率过高,存在内存分配风暴
\ No newline at end of file