在C#代码中实现并行处理 [英] Implementing parrallel processing in C# code

查看:49
本文介绍了在C#代码中实现并行处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个需要获取存储在某些专有数据库中的旧值的代码。

要获取数据,需要提供一些API和相关函数。代码如下。



现在,如果我运行此代码,则需要近355秒。我已经测量了一个点的执行时间,并且花费了大约10秒钟(以平均值计算)。

测试用大约37个这样的'pnt'完成,花了将近355秒我需要将相同的值导出到excel

。但是,与通过预定义的API从时间序列数据库中获取数据相比,导出到excel并不耗时。

我对API没有任何控制权,我也无法做任何事情。

考虑到这一切,我可以实现并行处理。我有24核CPU的机器需要运行程序。

如果有人可以告诉我如何实现同样在代码中它对我有帮助。

我正在阅读这方面的一些文章,但我似乎很难在代码中实现。

如果有的话一根手杖帮助我在这段代码中实现并行处理,这将有助于减少程序所花费的时间。



我尝试过:



I was trying to write one code that needs to fetch the old values stored in some proprietary data base.
To fetch the data, some API and associated functions are provided.The code is as follows.

Now if I run this code, it takes almost 355 seconds. I have measured the time of execution for one point and it took almost 10 seconds(in an avg) to do that.
The test was done with approximately 37 such 'pnt' and it took almost 355 seconds to fetch the required values.The I need to export the same to excel
also.But exporting to excel is not time consuming compared to fetching of data from a time series database through predefined APIs.
I do not have any control in the APIs and I can not do anything on the same.
Considering all this, can I implement parallel processing.I have machine with 24 cores CPU where the program needs to be run.
If somebody can tell me how to implement the same in the code it will be helpful for me.
I was reading some article in this regard, but it seems difficult for me to implement in the code.
If some one cane help me out implementing parallel processing in this code, it will be a great help in reducing time taken by the program.

What I have tried:

int i = 0, j = 0;
                string pnt = "";
                TimeSpan span;
                if (periodtime.SelectedIndex == 0)
                {
                    span = TimeSpan.FromSeconds(Convert.ToInt16(periodval.Text));
                }
                else if (periodtime.SelectedIndex == 1)
                {
                    span = TimeSpan.FromMinutes(Convert.ToInt16(periodval.Text));
                }
                else
                {
                    span = TimeSpan.FromHours(Convert.ToInt16(periodval.Text));
                }	
          	uint s = 0;
                double dval = 0;
                DateTime ts = DateTime.Now;
                string st = "";
                int nRet = 0;
foreach (string slpnt in pntid)
                {




//==========================================================================================
/*The point consist of a collection of strings separated by .,$ etc.It is defined inside a text file.The code reads the whole line and split a particular portion (required one) that we need to pass to API's function.*/
		  //That portion is stored in 'pnt' variable. 
                   pnt = slpnt.Split('-')[0].Split('$')[0];
                    i = 0;
                   span = TimeSpan.FromHours(24);
                   //GetHistMin is a function(Part of an application API) to fetch historical minimum values stored historixcal information database.Hence if .nRet =0 , it enters the loop.
//Here the time range is from Convert.ToDateTime(Fdt.Text + " 00:00:00" =00:00:00 hrs to 24:00:00 hrs (Convert.ToDateTime(Fdt.Text + " 00:00:00").AddHours(24)
/*Span is user given  and here it is 30 seconds.Please note that it is a large data base and for 24 hrs we approximately have to search 4000 samples for a pnt variable and get the minimum out of it.We need to fetch such 50 pnt in minimum to get the desired result.*/
nRet = History.GetHistMin(pnt, Convert.ToDateTime(Fdt.Text + " 00:00:00"), Convert.ToDateTime(Fdt.Text + " 00:00:00").AddHours(24), span, out s);
                   if (nRet == 0)
                   {
                       nRet = History.GetNextHist(s, out dval, out  ts, out st);
					   //GetNextHist is used to collect the resulting value the returned by GetHistMin(). 
                       if (j == 0)
                       {
//Doing the required calculation and adding it to new row of data table.
                           dr = dt.NewRow();
                           dr[0] = "Minimum(of the day)";
                           if (slpnt.Contains("$"))
                           {
                           dr["C" + j.ToString()] = Math.Round(dval, 2);

                           }
                           else
                           {
                           dr["C" + j.ToString()] = Math.Round(dval, 1);
                           }
                           dt.Rows.Add(dr);
                         }
                       else
                 {
                           if (slpnt.Contains("$"))
                 {
                dt.Rows[dt.Rows.Count - 1]["C" + j.ToString()] = Math.Round(dval, 2);
                 }
                           else
                 {
                dt.Rows[dt.Rows.Count - 1]["C" + j.ToString()] = Math.Round(dval, 1);
                 }
             }
         } 





// ========== ================================================ <无线电通信/>



//==========================================================

//Like Minimum, it is calculating the Maximum of the day value.
                  nRet = History.GetHistMax(pnt, Convert.ToDateTime(Fdt.Text + " 00:00:00"), Convert.ToDateTime(Fdt.Text + " 00:00:00").AddHours(24), span, out s);
                   if (nRet == 0)
                   {

                       nRet = History.GetNextHist(s, out dval, out  ts, out st);
                       if (j == 0)
                       {
                           dr = dt.NewRow();
                           dr[0] = "Maximum(of the day)";
                           if (slpnt.Contains("$"))
                           {
                               dr["C" + j.ToString()] = Math.Round(dval, 2);
                           }
                           else
                           {
                               dr["C" + j.ToString()] = Math.Round(dval, 1);
                           }
                           dt.Rows.Add(dr);
                           }
                       else
                       {
                           if (slpnt.Contains("$"))
                           {
         dt.Rows[dt.Rows.Count - 1]["C" + j.ToString()] = Math.Round(dval, 2);
                           }
                           else
                           {
         dt.Rows[dt.Rows.Count - 1]["C" + j.ToString()] = Math.Round(dval, 1);
                           }
                        }			   
                    }  



// ================== ================================================== ================================================== ===


//=========================================================================================================================

//Like Minimum, it is calculating the Average of the day value.
                    nRet = History.GetHistAvg(pnt, Convert.ToDateTime(Fdt.Text + " 00:00:00"), Convert.ToDateTime(Fdt.Text + " 00:00:00").AddHours(24), span, out s);
                    
                    if (nRet == 0)
                    {
                        nRet = History.GetNextHist(s, out dval, out  ts, out st);
                        if (j == 0)
                        {
                            dr = dt.NewRow();
                            dr[0] = "Average(of the day)";
                            if (slpnt.Contains("$"))
                            {
                                dr["C" + j.ToString()] = Math.Round(dval, 2);
                            }
                            else
                            {
                                dr["C" + j.ToString()] = Math.Round(dval, 1);
                            }
                            dt.Rows.Add(dr);
                        }
                        else
                        {
                            if (slpnt.Contains("$"))
                            {
              dt.Rows[dt.Rows.Count - 1]["C" + j.ToString()] = Math.Round(dval, 2);
                            }
                            else
                            {
              dt.Rows[dt.Rows.Count - 1]["C" + j.ToString()] = Math.Round(dval, 1);
                            }
                        }
                    }
	j++;
}

推荐答案

等。它在文本文件中定义。代码读取整行并拆分特定部分(必需的)我们需要传递给API的函数。* /
//该部分存储在'pnt'变量中。
pnt = slpnt.Split(' - ')[0] .Split('
etc.It is defined inside a text file.The code reads the whole line and split a particular portion (required one) that we need to pass to API's function.*/ //That portion is stored in 'pnt' variable. pnt = slpnt.Split('-')[0].Split('


')[0];
i = 0;
span = TimeSpan.FromHours(24);
// GetHistMin是一个函数(应用程序API的一部分),用于获取存储在历史记录信息数据库中的历史最小值。如果.nRet = 0,则它进入循环。
//这里的时间范围是Convert.ToDateTime(Fdt.Text +00:00:00= 00:00:00hrs到24:00:00 hrs(Convert.ToDateTime(Fdt.Text +) 00:00:00)。AddHours(24)
/ * Span是用户给定的,这里是30秒。请注意它是一个大型数据库,24小时我们大约需要搜索4000个样本对于一个pnt变量并获得最小值。我们需要获取最小50 pnt以获得所需的结果。* /
nRet = History.GetHistMin(pnt,Convert.ToDateTime(Fdt.Text +) 00:00:00),Convert.ToDateTime(Fdt.Text +00:00:00)。AddHours(24),span,out s);
if(nRet == 0)
{
nRet = History.GetNextHist(s,out dval,out ts,out st);
// GetNextHist用于收集GetHistMin()返回的结果值。
if (j == 0)
{
//执行所需的计算并将其添加到新的数据表行。
dr = dt.NewRow();
dr [0] =最低(当天);
if(slpnt.Contains(
')[0]; i = 0; span = TimeSpan.FromHours(24); //GetHistMin is a function(Part of an application API) to fetch historical minimum values stored historixcal information database.Hence if .nRet =0 , it enters the loop. //Here the time range is from Convert.ToDateTime(Fdt.Text + " 00:00:00" =00:00:00 hrs to 24:00:00 hrs (Convert.ToDateTime(Fdt.Text + " 00:00:00").AddHours(24) /*Span is user given and here it is 30 seconds.Please note that it is a large data base and for 24 hrs we approximately have to search 4000 samples for a pnt variable and get the minimum out of it.We need to fetch such 50 pnt in minimum to get the desired result.*/ nRet = History.GetHistMin(pnt, Convert.ToDateTime(Fdt.Text + " 00:00:00"), Convert.ToDateTime(Fdt.Text + " 00:00:00").AddHours(24), span, out s); if (nRet == 0) { nRet = History.GetNextHist(s, out dval, out ts, out st); //GetNextHist is used to collect the resulting value the returned by GetHistMin(). if (j == 0) { //Doing the required calculation and adding it to new row of data table. dr = dt.NewRow(); dr[0] = "Minimum(of the day)"; if (slpnt.Contains("


))
{
dr [C+ j.ToString()] = Math.Round (dval,2);

}
其他
{
dr [C+ j.ToString()] = Math.Round(dval,1);
}
dt.Rows.Add(dr);
}
其他
{
if(slpnt.Contains(
")) { dr["C" + j.ToString()] = Math.Round(dval, 2); } else { dr["C" + j.ToString()] = Math.Round(dval, 1); } dt.Rows.Add(dr); } else { if (slpnt.Contains("


这篇关于在C#代码中实现并行处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆