文件: root - text - computing - delphi - 2011 - 06 - tricks.txt
标签: 计算机, Computing Tips, | 英文 | 主页 | 类别: 计算机科学 | 778 次阅读, 25746 次搜索 | 142 个单词
| 浏览 | 博客存档
标签: 计算机, Computing Tips, | 英文 | 主页 | 类别: 计算机科学 | 778 次阅读, 25746 次搜索 | 142 个单词
| 浏览 | 博客存档
Two tricks of making integer computation faster in Delphi.
1. Two integers a and b, to compute a mod b, where b is power of two (two, four, eight ... etc)...
in delphi, you write
result := a mod b;
in this case, to make it faster,
you can write
result := a and (b - 1); // b >= 2, and power of 2.
2. Two integers a (a >= 0) and b, to compute a div b, where b is power of two (two, four, eight ... etc)...
in delphi, you write
result := a div b;
in this case, to make it faster,
you can write
c := fun(b); // fun returns the integer(log2(b))
result := a shr c; // where b >= 2, and power of 2
// returns the left-most significant digit.
function fun(b: integer): integer; assembler; register;
asm
bsr eax, eax
end;
if (a < 0), it can be reworked as result = -((-a) shr c)
标签: 计算机, Computing Tips, | 英文 | 主页 | 类别: 计算机科学 | 778 次阅读, 25746 次搜索 | 142 个单词 1. Two integers a and b, to compute a mod b, where b is power of two (two, four, eight ... etc)...
in delphi, you write
result := a mod b;
in this case, to make it faster,
you can write
result := a and (b - 1); // b >= 2, and power of 2.
2. Two integers a (a >= 0) and b, to compute a div b, where b is power of two (two, four, eight ... etc)...
in delphi, you write
result := a div b;
in this case, to make it faster,
you can write
c := fun(b); // fun returns the integer(log2(b))
result := a shr c; // where b >= 2, and power of 2
// returns the left-most significant digit.
function fun(b: integer): integer; assembler; register;
asm
bsr eax, eax
end;
if (a < 0), it can be reworked as result = -((-a) shr c)
猜您喜欢...
- Non-decreasing Array with Single Modification
- Top K Frequent words
- Skip the readings, focus on problems. And use all the hints!
- Algorithm Interview Question: Symmetric k-ary Tree
- Kaprekar
- Algorithm Interview Question: Find the Single Element in an Array of Duplicates
- CPU Utilization
- YES!!
- Daily Interview Problem: Merge K Sorted Linked Lists
- Spreadsheet Column Title
©2006~2024 牛排过熟 - 0.01785 秒 - 1065.958 KB/s - 19 在线 内存: 496.52 KB
18:54:01 up 13 days, 18:33, 2 users, load average: 0.98, 0.86, 0.73 - 服务器 PHP 版本号: 7.4.33
牛排怎么做才好吃? | <meta name="机器人" content="索引, 跟踪">
18:54:01 up 13 days, 18:33, 2 users, load average: 0.98, 0.86, 0.73 - 服务器 PHP 版本号: 7.4.33
评论 (0)
读写完全 - 一般 - 最小 - 表格 - 所有评论 - 统计
当前页暂时没有评论。