javascript - 如何增加并发异步调用的数量?

我正在尝试增加可以在C#中进行的并发异步调用的数量。具体来说,我试图匹配在Node.js中看到的性能。

以下是两个示例程序,一个使用C#,另一个使用JavaScript。两者都从包含随机数的10MB文件中将随机数的字节读入缓冲区,然后异步执行此操作2000次。 JavaScript程序每秒可以在我的计算机上执行约60,000次此操作,但是C#程序只能执行约3,000次。据我所知,他们在做相同的事情,但是我知道肯定存在一些差异。

我已经使用不同的功能运行了类似的测试,包括睡眠很少的时间,并使用Datastax Cassandra驱动程序对Cassandra集群执行插入操作。后一种功能是我真正想要改进的功能,因为在C#中我每秒只能获得大约80次插入,而在Node中只能获得大约5000次插入。

有人可以帮我解释这个差异吗?

// C#

class Program
{
    static Random r = new Random();

    static void Main(string[] args)
    {
        int iterations = 2000;

        Stopwatch s = new Stopwatch();
        s.Start();

        var tasks = Enumerable.Range(1, iterations).Select(i => ReadFile()).ToArray();
        Task.WaitAll(tasks);

        s.Stop();

        Console.WriteLine("Total elapsed milliseconds was {0}.", s.ElapsedMilliseconds);
        Console.WriteLine("Total iterations were {0}.", iterations);
        Console.WriteLine("Total iterations per second was {0}.", iterations / s.Elapsed.TotalSeconds);
    }

    static async Task ReadFile()
    {
        string path = @"C:\Temp\random.txt";
        int readSize = r.Next(512, 10 * 1024);

        using (StreamReader reader = new StreamReader(path))
        {
            await reader.ReadAsync(new char[readSize], 0, readSize);
        }
    }
}




// JavaScript

var fs = require('fs');
var now = require('performance-now');

var iterations = 2000;
var startTime = now();
var runningIterations = 0;

for (var i = 1; i <= iterations; i++) {
    readFile(function () {
        runningIterations++;

        if (iterations == runningIterations) {
            totalTime = (now() - startTime);
            console.log("Total iterations: " + iterations);
            console.log("Total time: " + totalTime);
            console.log("Iterations per second: " + (iterations / (totalTime / 1000)));
        }
    });
}

function readFile(callback) {
    var path = "/Temp/random.txt";

    fs.open(path, 'r', function (err, fd) {
        var readSize = Math.floor(Math.random() * 10 * 1024) + 512;
        var buffer = new Buffer(readSize);
        fs.read(fd, buffer, 0, readSize, 0, function (err, bytesRead, buffer) {
            callback();
        });
    });
};

最佳答案

考虑到以下情况,我不确定我是否相信C#速度较慢:

运行您的节点脚本,我收到:

Total iterations: 2000
Total time: 1199.8234639999998
Iterations per second: 1666.9118916314178


按原样运行您的C#脚本,我收到:

Total elapsed milliseconds was 1041.
Total iterations were 2000.
Total iterations per second was 1920.61576477911.


如果我将其重构为使用Parallel.For

static Random r = new Random();

void Main()
{
     int iterations = 2000;

    Stopwatch s = new Stopwatch();
    s.Start();

    Parallel.For(0, iterations, ReadFile);

    s.Stop();

    Console.WriteLine("Total elapsed milliseconds was {0}.", s.ElapsedMilliseconds);
    Console.WriteLine("Total iterations were {0}.", iterations);
    Console.WriteLine("Total iterations per second was {0}.", iterations / s.Elapsed.TotalSeconds);
}

// Define other methods and classes here
static async void ReadFile(int x)
{
    string path = @"C:\Temp\random.txt";
    int readSize = r.Next(512, 10 * 1024);
    using (StreamReader reader = new StreamReader(path))
    {
        await reader.ReadAsync(new char[readSize], 0, readSize);
    }
}


我获得了相当可观的性能提升:

Total elapsed milliseconds was 389.
Total iterations were 2000.
Total iterations per second was 5134.14889288497.


我还押注,如果我缓存了缓冲区,那么在所有情况下,都会有另一个实质性的问题。但是,我只能按本地计算机上的性能进行测试。而且,从我看来,没有什么让我觉得“哦,哇,有些事要走了”。我也不知道你的环境。 :耸耸肩:

本文翻译自 https://stackoverflow.com/questions/32148261/

网站遵循 CC BY-SA 4.0 协议,转载或引用请注明出处。

标签 javascript c# node.js asynchronous


相关文章:

javascript - 用AJAX提交动态表单? [重复]

javascript - 将Canvas Arc划分为364天

javascript - 单击时的备用按钮颜色

javascript - 将ES6 Iterable转换为Array

javascript - 使JQueryMobile表可滚动?

javascript - 使用带有两个参数的函数的javascript映射

javascript - 绝对位置和相对位置都不起作用

javascript - 将拉丁数字更改为波斯数字,并通过jQuery函数添加逗号

javascript - 嵌套函数调用javascript中的“ click”事件[重复]

javascript - 如何从选项集中获取所选值crm 4 dynamics 2011在线