C# WPF上位机开发(函数运行时间分析)

【 声明:版权所有,欢迎转载,请勿用于商业用途。 联系信箱:feixiaoxing @163.com】

上位机除了基本功能和稳定性之外,还有一个要注意的就是运行效率的问题。如果我们想提高软件的运行效率,单位时间做更多的工作,或者是希望在最短的时间内完成某一个工作,这个时候就要优化一下函数的使用时间了。

测量的方法一般也比较简单,一般就是函数开头或者thread开头添加一个时间戳,在任务结束的地方也添加一个时间戳,两个时间戳的减法就是整个程序花费的时间。

1、设计界面

为了测试方便,我们画了一个界面,界面上面只有一个按钮,这样比较简单一点。

复制代码
<Window x:Class="WpfApp.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:sys="clr-namespace:System;assembly=mscorlib"
        xmlns:local="clr-namespace:WpfApp"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="600">
    <Grid>
        <StackPanel>
            <Button Content="Check Elapsed Time" Width="200" Height="80" Click="Button_Click" HorizontalAlignment="Center" VerticalAlignment="Top" Margin="100,50,100,0"/>
        </StackPanel>
    </Grid>
</Window>

对应的显示效果如下所示,

2、准备和实现代码

代码部分首先我们需要添加一个函数库,也就是Diagnostics库,这里面有我们需要的Stopwatch结构体。这个结构体大家可以看成是一个秒表。如果按下去,开始计时。再按下去的时候,停止计时。这个时候,我们再看一下秒表,就知道函数执行花费了多少时间了。

为了测试花费了多少时间,我们还添加了一个DoSomeOperation函数。在实际应用中,我们将它替换成自己需要测试的thread或者函数就可以了。

复制代码
using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using System.Windows;

using System.Diagnostics;

namespace WpfApp
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {

        // construct function
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            // create Stopwatch instance
            Stopwatch stopwatch = new Stopwatch();

            // begin to start
            stopwatch.Start();

            // do some operation
            DoSomeOperation();

            // stop now
            stopwatch.Stop();

            // output final result
            MessageBox.Show($"Execute Time: {stopwatch.ElapsedMilliseconds} ms");
        }

        private void DoSomeOperation()
        {
            // simulate some operation
            for (int i = 0; i < 100000000; i++)
            {
                // some cpu operation
            }
        }
    }
}

3、效果测试和验证

测试的话,比较简单,直接编译之后,单击按钮即可。

相关推荐
A_nanda9 小时前
c# MOdbus rto读写串口,如何不相互影响
算法·c#·多线程
码云数智-园园11 小时前
使用 C# 将 PowerPoint 演示文稿高效转换为 PDF 格式
c#
听麟11 小时前
HarmonyOS 6.0+ 智慧出行导航APP开发实战:离线地图与多设备位置协同落地
华为·wpf·harmonyos
PfCoder12 小时前
WinForm真入门(23)---PictureBox 控件详细用法
开发语言·windows·c#·winform
gc_229915 小时前
C#学习调用OpenMcdf模块解析ole数据的基本用法(1)
c#·ole·openmcdf
AC赳赳老秦15 小时前
DeepSeek 辅助科研项目申报:可行性报告与经费预算框架的智能化撰写指南
数据库·人工智能·科技·mongodb·ui·rabbitmq·deepseek
Dontla18 小时前
Axure RP(Rapid Prototyper)原型图设计工具介绍
ui·axure·photoshop
晚霞的不甘18 小时前
Flutter for OpenHarmony从基础到专业:深度解析新版番茄钟的倒计时优化
android·flutter·ui·正则表达式·前端框架·鸿蒙
MM_MS19 小时前
Halcon图像点运算、获取直方图、直方图均衡化
图像处理·人工智能·算法·目标检测·计算机视觉·c#·视觉检测
笨蛋不要掉眼泪19 小时前
Spring Boot + RedisTemplate 数据结构的基础操作
java·数据结构·spring boot·redis·wpf