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、效果测试和验证

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

相关推荐
mudtools18 小时前
.NET驾驭Word之力:玩转文本与格式
c#·.net
唐青枫1 天前
C#.NET 数据库开发提速秘籍:SqlSugar 实战详解
c#·.net
mudtools2 天前
.NET驾驭Word之力:理解Word对象模型核心 (Application, Document, Range)
c#·.net
大飞pkz2 天前
【设计模式】C#反射实现抽象工厂模式
设计模式·c#·抽象工厂模式·c#反射·c#反射实现抽象工厂模式
Yasin Chen2 天前
Unity UI坐标说明
ui·unity
唐青枫2 天前
从入门到进阶:C#.NET Stopwatch 计时与性能测量全攻略
c#·.net
未来之窗软件服务2 天前
幽冥大陆(二)RDIFSDK 接口文档:布草洗涤厂高效运营的技术桥梁C#—东方仙盟
开发语言·c#·rdif·仙盟创梦ide·东方仙盟
1uther2 天前
Unity核心概念⑨:Screen
开发语言·游戏·unity·c#·游戏引擎
阿幸软件杂货间2 天前
Office转PDF转换器v1.0.py
开发语言·pdf·c#
sali-tec2 天前
C# 基于halcon的视觉工作流-章34-环状测量
开发语言·图像处理·算法·计算机视觉·c#