Home > Net >  In intensive computing tasks, can really achieve multi-threaded "and" run the calculation?
In intensive computing tasks, can really achieve multi-threaded "and" run the calculation?

Time:04-15

With signature of a function, because every so often a sample is signed multiple data at the same time, under the assumption that each signature need to 10 ms of time, I need a signed article 10 data, order signature takes 100 ms, I drive 10 is arguably a TASK at the same time signature if CPU resources under the condition of "enough" is arguably the TASK completion time should add a little loss, is a 10 ms, but in fact the TASK completion time can sometimes even more than 100 ms (sequence) is the sum of the time, and then to check the information, found that many languages have thread lock, actually just "concurrent", not "parallel" in computing,
To verify whether the hardware resources problem, I try to use multiple processes running the same row with the signature of the single thread process, the result also seems to verify the threads just run concurrently, (multiple processes, each process using the signature of the time is 10 ms, of course there are the wastage of the error and CPU),,


So, I would like to ask two questions

1, c # can achieve real "parallel" multithreaded execution?

2, if you can't parallel processing, so I'm going to solve with way of multiple processes, so could you please tell me in order to reduce the delay between two processes, in that multiple interprocess communication way, please? Probably checked the, ipc, Shared memory, etc.,,, I am in. Net in the core, and development under the Windows program debugging, running under Linux, that is to say, it is best to compatible with two kinds of systems, and communication delay to the lower, the better, at least in milliseconds, excuse me I should focus on to find what kind of information?

CodePudding user response:

This problem take you on a, what you said is very right
Thread is a kind of using short time switch concurrent execution, cause parallel method in image (because the CPU operation is fast and a time slice is only a few nanoseconds, so the senses he is continuous execution)
Of course involve the thread, so the more you join the thread, distribution of the total execution time of each thread will be extended accordingly
So they produce the multi-core operation, and in the netcore, multi-core operation for the Parallel Parallel computing, he is a support for multi-core CPU optimization, which is as far as possible put your thread task among different CPU, so he is the real execution at the same time

To supplement your last post Gc effects
1. Most of the discussion of gc impact on the process of discussion, occur in the program takes up a large amount of physical memory, that is, two types of
A: you program quickly produce a large number of 1 generation of objects, and are using gc can't give you the release
2: your program has serious memory leaks, be outside the scope of the gc controls

So if your program more than 800 m memory, and in the above two situations, gc is a clean, can only take 1 to 2 m, at this point in the application of memory, will force the gc to clean up, as a gc actions frequently, almost every time to clean up, so will slow

This things, we see our disk C, if just installed disk C 60 g, left me 3 months to clean up time is ok, because he enough points for a moment and march to clean up a release of a 20 g into
But if, over time we put one more, just left 6-12 g, I'm just a few days to clear once all not line, by that time, basically disk C is we must, in cleaning up please clear out what, even if I clean up every day, will keep on 8 g,

So strict that gc has influence to the program, but he took place in the total physical memory takes up more than 90%, the gc must each action to clean it again, so in front of the conclusion, we should first discuss thing is why there is so much gc qing motionless,, yeah, the attitude of disk C, if takes up more than 90%, we can only go to see the bottom plate installed C what, why C dish there are so many, not clear, also dare not clear, or I don't know him with something -- -- -- -- -- -- -- -- discharge (within) load (save) heritage (drain) leave (leak)

CodePudding user response:

Actually your problem, we are more split task

Now that we know the multi-threaded switch will consume time, so we need to split into many small tasks, these small tasks he can go quickly, go away, don't participate in

Another is reasonable dispatch IO task and computing tasks, IO task generally use less CPU, computing tasks to consume CPU

So we calculated for you such signature such thing, we usually write now is

Using (steam sm=awaite asynchronous I/o to give him a stream)
{
In front of the xx=await the response calculation (sm)
String=XXX await response. GetStringAsync ()
}

This way, he is actually three smaller thread, before and after the two is the IO task, the middle is computing task, of course, these things because of does not involve UI part, so you can use the ConfigureAwait (false), let him not to process in the subsequent mission to capture context, is to speed up the switch

CodePudding user response:

Parallel computing can shorten the time, also want to see the task itself,
A single task if you have will you eat is full, the CPU is also does not make sense, parallel computing,
But if that's the following case, parallel have obvious advantage:
The task itself does not consume computing resources, but takes longer, such as the need to wait for the task, a common view advertising page (such as 90 seconds), integral task, if you have 1000 account to do the task, sequential execution time probably is 90 * 1000 seconds; Parallel computing is bound to fast very much, can be infinitely close to 90 s, even after optimization

CodePudding user response:

The parallel. The invoke ()
I remember it can be executed in parallel

CodePudding user response:

Words, the actual test, I actually didn't see you said
The test code

 static async Task Main (string [] args) 
{

String x=string. Join (" ", Enumerable. Repeat (1, 100));
A Stopwatch watch=new Stopwatch ();
Watch. Start ();


Await Task. WhenAll (getMD5 (x), getMD5 (x), getMD5 (x), getMD5 (x), getMD5 (x), getMD5 (x), getMD5 (x), getMD5 (x),
, getMD5 getMD5 (x) (x)
);
Watch. Stop ();
Console. WriteLine (watch. Elapsed. TotalMilliseconds);


Console.ReadKey();
}
The static MD5 MD5=MD5. The Create ();
Public static async Task GetMD5 (string STR)
{

Using (Stream ms=await CreateStrem (STR). ConfigureAwait (false))
{

Var x=await Task{

Return to md5.Com puteHash (ms);
}). ConfigureAwait (false);

Return UTF8Encoding. And UTF8. Get string (x);
}

}

Public static Task CreateStrem (string x)
{
Return Task The Run (async ()=& gt;
{
MemoryStream ms=new MemoryStream();
Await Ms. WriteAsync (UTF8Encoding. And UTF8 GetBytes (x));
Ms. Seek (0, SeekOrigin. Begin);
Return (Stream) ms;


});


}


In addition to statistical output, I didn't join any other output, because the console output may interfere with the statistics,

The actual result: separate operation a and concurrent 10 no difference, I test machine to run a 22 ms, run 10 and 22 ms, actually 22 ms for computer is without pressure, to fast, go too fast (elementary school: a water inlet, a water and 22 ms for computer is almost equal to the rhythm of water, the water)

Ps: ms. 22 is run directly in the console, not run in the vs, vs run almost * 10, because vs start diagnosis etc. Additional overhead

CodePudding user response:

 await Task. WhenAll (getMD5 (x), getMD5 (x), getMD5 (x), getMD5 (x), getMD5 (x), getMD5 (x), getMD5 (x), getMD5 (x), 
, getMD5 getMD5 (x) (x), getMD5 (x), getMD5 (x), getMD5 (x), getMD5 (x), getMD5 (x), getMD5 (x), getMD5 (x),
, getMD5 getMD5 (x) (x), getMD5 (x), getMD5 (x), getMD5 (x), getMD5 (x), getMD5 (x), getMD5 (x), getMD5 (x),
, getMD5 getMD5 (x) (x), getMD5 (x), getMD5 (x), getMD5 (x), getMD5 (x), getMD5 (x), getMD5 (x), getMD5 (x),
, getMD5 getMD5 (x) (x), getMD5 (x), getMD5 (x), getMD5 (x), getMD5 (x), getMD5 (x), getMD5 (x), getMD5 (x),
, getMD5 getMD5 (x) (x), getMD5 (x), getMD5 (x), getMD5 (x), getMD5 (x), getMD5 (x), getMD5 (x), getMD5 (x),
, getMD5 getMD5 (x) (x), getMD5 (x), getMD5 (x), getMD5 (x), getMD5 (x), getMD5 (x), getMD5 (x), getMD5 (x),
, getMD5 getMD5 (x) (x), getMD5 (x), getMD5 (x), getMD5 (x), getMD5 (x), getMD5 (x), getMD5 (x), getMD5 (x),
, getMD5 getMD5 (x) (x), getMD5 (x), getMD5 (x), getMD5 (x), getMD5 (x), getMD5 (x), getMD5 (x), getMD5 (x),
, getMD5 getMD5 (x) (x), getMD5 (x), getMD5 (x), getMD5 (x), getMD5 (x), getMD5 (x), getMD5 (x), getMD5 (x),
nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  •  Tags:  
  • C#
  • Related