划分工作,将任务分配给线程数组 [英] Divide work, assign task to an array of threads

查看:91
本文介绍了划分工作,将任务分配给线程数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要对备忘录中的文本进行数学计算. [文件大小:〜2mb]

I need to do math calculations for a text in a memo. [Size of the file: ~2mb]

一个合适的例子是我需要解码的编码文本.

A fitting example would be an encoded text that I need to decode.

我将备忘录文本传递给字符串以对其进行解码. 我猜想使用线程运行我的解码功能会更快. 但是经过一番谷歌搜索后,我没有找到适合我目的的好例子.

I pass the memo text to a string in order to decode it. I guess it would be way faster to run my decode function using threads. But after some googling I didn't find a good example fitting my purpose.

示例功能:

function entr_base_N(my_text:String):String;
var
    ts_hamil64:Integer;
begin
    For ts_hamil64 := 1 to Length(my_text) do
    begin
         Result:= Result + Chr(Ord(my_text[ts_hamil64])+10)
    end;    
end;
.....
.....
Memo1.Text:=entr_base_N(Memo1.Text)

我想将工作分解成小块,将工作平均分配,比如说3..8个线程,然后将我的解码函数分配给这些线程.你能指导我吗?

I would like to break the work into small pieces, divide the job equally, lets say 3..8 threads and assign my decode function to those threads. Can you please guide me on this?

当前处理文本文件的时间:〜35秒. 谢谢您的帮助.

Current time to process the text file: ~35seconds. Thank you for your kind help.

推荐答案

线程不是问题.您的函数entr_base_N立即运行.尝试在调试器中.您会发现它根本不需要时间.在现代计算机上,处理2MB字符串是微不足道的.也就是说,我总是建议在可能的情况下预先分配一个返回缓冲区.

Threading is not the problem. Your function entr_base_N runs instantly. Try inside the debugger. You'll find it takes no time at all. Processing a 2MB string is trivial on a modern computer. That said, I would always recommend pre-allocating a return buffer when possible.

所有时间都花在了将结果字符串发送回备忘录控件上.发生的是,您正在将#13和#10字符转换为#23和#20.无论出于何种原因,备忘录控件都不喜欢这样.在我看来,您发回的字符串根本没有换行,并且备忘录的自动换行代码性能很差.

All the time is spent sending the resulting string back to the memo control. What is happening is that you are converting the #13 and #10 characters to #23 and #20. For whatever reason, the memo control does not like that. It seems to me that you are sending back a string with no line feeds at all and the memo's word wrap code performs badly.

一种快速而又肮脏的方法是在备忘录中将WordWrap设置为False.

A quick and dirty way to see that this is so is to set WordWrap to False on your memo.

这里重要的一课是,在尝试优化之前,您必须正确识别瓶颈.这是一个容易陷入的陷阱,正如我最初为回答这个问题所做的摸索工作所证明的那样.

The important lesson here is that you must correctly identify the bottleneck before attempting to optimise. It's an easy trap to fall into though, as my initial fumbled efforts to answer this question demonstrate.

这篇关于划分工作,将任务分配给线程数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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