첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

■ DataGrid 클래스에서 데이터 업데이트 성능을 측정하는 방법을 보여준다.

TestProject.zip
0.01MB

▶ SampleItem.cs

using System.ComponentModel;

namespace TestProject
{
    /// <summary>
    /// 샘플 항목
    /// </summary>
    public class SampleItem : INotifyPropertyChanged
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Event
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 속성 변경시 이벤트 - PropertyChanged

        /// <summary>
        /// 속성 변경시 이벤트
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 매도
        /// <summary>
        private int selling;

        /// <summary>
        /// 값
        /// <summary>
        private int value;

        /// <summary>
        /// 매수
        /// <summary>
        private int buying;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Property
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 매도 - Selling

        /// <summary>
        /// 매도
        /// </summary>
        public int Selling
        {
            get
            {
                return this.selling;
            }
            set
            {
                if(this.selling == value)
                {
                    return;
                }

                this.selling = value;

                FirePropertyChangedEvent(nameof(Selling));
            }
        }

        #endregion
        #region 값 - Value

        /// <summary>
        /// 값
        /// </summary>
        public int Value
        {
            get
            {
                return this.value;
            }
            set
            {
                if(this.value == value)
                {
                    return;
                }

                this.value = value;

                FirePropertyChangedEvent(nameof(Value));
            }
        }

        #endregion
        #region 매수 - Buying

        /// <summary>
        /// 매수
        /// </summary>
        public int Buying
        {
            get
            {
                return this.buying;
            }
            set
            {
                if(this.buying == value)
                {
                    return;
                }

                this.buying = value;

                FirePropertyChangedEvent(nameof(Buying));
            }
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Protected

        #region 속성 변경시 이벤트 발생시키기 - FirePropertyChangedEvent(propertyName)

        /// <summary>
        /// 속성 변경시 이벤트 발생시키기
        /// </summary>
        /// <param name="propertyName">속성명</param>
        protected void FirePropertyChangedEvent(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        #endregion
    }
}

 

▶ MainWindow.xaml

<Window x:Class="TestProject.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Width="800"
    Height="600"
    Title="DataGrid 클래스 : 데이터 업데이트 성능 측정하기"
    FontFamily="나눔고딕코딩"
    FontSize="16">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*"    />
            <RowDefinition Height="Auto" />
        </Grid.RowDefinitions>
        <DataGrid Name="dataGrid" Grid.Row="0"
            Margin="10 10 10 0"
            AutoGenerateColumns="False"
            IsReadOnly="True">
            <DataGrid.Columns>
                <DataGridTextColumn Header="매도"
                    Width="150"
                    Binding="{Binding Selling}" />
                <DataGridTextColumn Header="값"
                    Width="150"
                    Binding="{Binding Value}" />
                <DataGridTextColumn Header="매수"
                    Width="150"
                    Binding="{Binding Buying}" />
            </DataGrid.Columns>
        </DataGrid>
        <Button Name="startButton" Grid.Row="1"
            Margin="0 10 10 10"
            HorizontalAlignment="Right"
            Width="100"
            Height="30"
            Content="시작" />
        <TextBlock Name="timeTextBlock" Grid.Row="1"
            HorizontalAlignment="Left"
            VerticalAlignment="Center"
            Margin="10 10 0 0" />
        <TextBlock Name="countTextBlock" Grid.Row="1"
            HorizontalAlignment="Center"
            VerticalAlignment="Center" />
    </Grid>
</Window>

 

▶ MainWindow.xaml.cs

using System;
using System.Windows;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows.Threading;

namespace TestProject
{
    /// <summary>
    /// 메인 윈도우
    /// </summary>
    public partial class MainWindow : Window
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 딕셔너리
        /// </summary>
        private SortedDictionary<int, SampleItem> dictionary;

        /// <summary>
        /// 난수기
        /// </summary>
        private Random random;

        /// <summary>
        /// 시작 값
        /// </summary>
        private const int startValue = 1000;

        /// <summary>
        /// 종료 값
        /// </summary>
        private const int endValue = 1050;

        /// <summary>
        /// 이전 값 배열
        /// </summary>
        private int[] previousValueArray = null;

        /// <summary>
        /// 타이머
        /// </summary>
        DispatcherTimer timer = new DispatcherTimer();

        /// <summary>
        /// 카운트
        /// </summary>
        private int count = 0;

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - MainWindow()

        /// <summary>
        /// 생성자
        /// </summary>
        public MainWindow()
        {
            InitializeComponent();

            this.dictionary = new SortedDictionary<int, SampleItem>();

            for(int i = startValue; i < endValue; i++)
            {
                this.dictionary.Add(i, new SampleItem { Selling = 0, Value = i, Buying = 0 });
            }

            this.random = new Random(DateTime.Now.Millisecond);

            this.dataGrid.ItemsSource = this.dictionary.Values;

            this.timer = new DispatcherTimer();

            this.timer.Interval = TimeSpan.FromMilliseconds(0.000001);

            this.timer.Tick        += timer_Tick;
            this.startButton.Click += startButton_Click;
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Private
        //////////////////////////////////////////////////////////////////////////////// Event

        #region 타이머 틱 처리하기 - timer_Tick(sender, e)

        /// <summary>
        /// 타이머 틱 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void timer_Tick(object sender, EventArgs e)
        {
            this.countTextBlock.Text = this.count.ToString();

            Stopwatch watch = new Stopwatch();

            watch.Start();

            for(int i = 0; i < 10; i++)
            {
                count++;

                if(this.previousValueArray != null)
                {
                    for(int j = 0; j < 40; j++)
                    {
                        int value = this.previousValueArray[j];

                        SampleItem item = this.dictionary[value];

                        item.Selling = 0;
                        item.Buying  = 0;
                    }
                }

                int[] valueArray = GetRandomValueArray();

                for(int j = 0; j < 40; j++)
                {
                    int value = valueArray[j];

                    SampleItem item = this.dictionary[value];

                    item.Selling = this.random.Next(1, 100);
                    item.Buying  = this.random.Next(1, 100);
                }

                this.previousValueArray = valueArray;
            }

            watch.Stop();

            this.timeTextBlock.Text = watch.Elapsed.ToString();
        }

        #endregion
        #region 시작 버튼 클릭시 처리하기 - testButton_Click(sender, e)

        /// <summary>
        /// 시작 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void startButton_Click(object sender, RoutedEventArgs e)
        {
            if(this.timer.IsEnabled)
            {
                this.timer.Stop();

                this.startButton.Content = "시작";
            }
            else
            {
                this.count = 0;

                this.timer.Start();

                this.startButton.Content = "중단";
            }
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////// Function

        #region 임의 값 배열 구하기 - GetRandomValueArray()

        /// <summary>
        /// 임의 값 배열 구하기
        /// </summary>
        /// <returns>임의 값 배열</returns>
        private int[] GetRandomValueArray()
        {
            int value = this.random.Next(startValue, 1011);

            int[] valueArray = new int[50];

            for(int i = 0; i < 40; i++)
            {
               valueArray[i] = value + i;
            }

            return valueArray;
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요