我正在玩C#,想加快一个程序.我做了改变,并且能够这样做.但是,我需要帮助理解为什么变化使它变得更快.
我试图将代码简化为更容易理解的问题.Score1和Report1是较慢的方式.Score2和Report2是更快的方式.第一种方法首先并行地在结构中存储字符串和int.接下来,在串行循环中,它循环遍历这些结构的数组并将其数据写入缓冲区.第二种方法首先将数据并行写入字符串缓冲区.接下来,在串行循环中,它将字符串数据写入缓冲区.以下是一些示例运行时间:
运行1总平均时间= 0.492087秒运行2总平均时间= 0.273619秒
当我使用早期的非并行版本时,时间几乎相同.为什么与并行版本的区别?
即使我减少Report1中的循环以将单行输出写入缓冲区,它仍然较慢(总时间约为.42秒).
这是简化的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading.Tasks;
using System.IO;
namespace OptimizationQuestion
{
class Program
{
struct ValidWord
{
public string word;
public int score;
}
ValidWord[] valid;
StringBuilder output;
int total;
public void Score1(string[] words)
{
valid = new ValidWord[words.Length];
for (int i = 0; i < words.Length; i++)
{
StringBuilder builder = new StringBuilder();
foreach (char c in words[i])
{
if (c != 'U')
builder.Append(c);
} …Run Code Online (Sandbox Code Playgroud) 运行pip install bitarray导致我的致命错误:
bitarray/_bitarray.c:9:20: fatal error: Python.h: No such file or directory
Run Code Online (Sandbox Code Playgroud)
我正在运行Ubuntu 12.04升级.也许我错过了安装一些依赖项?
运行Python工作:
Python 2.7.3 (default, Aug 1 2012, 05:16:07)
[GCC 4.6.3] on linux2
Run Code Online (Sandbox Code Playgroud)