文件: root - text - computing - delphi - 2011 - 06 - tricks.txt
标签: 计算机, Computing Tips, | 英文 | 主页 | 类别: 计算机科学 | 999 次阅读, 27834 次搜索 | 142 个单词
| 浏览 | 博客存档
标签: 计算机, Computing Tips, | 英文 | 主页 | 类别: 计算机科学 | 999 次阅读, 27834 次搜索 | 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, | 英文 | 主页 | 类别: 计算机科学 | 999 次阅读, 27834 次搜索 | 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)
猜您喜欢...
- Making a Height Balanced Binary Search Tree
- Longest Substring Without Repeating Characters
- Binary Tree Level with Minimum Sum
- [Daily Problem] Angles of a Clock
- Daily Interview Problem: Min Range Needed to Sort
- Daily Interview Problem: Deepest Node in a Binary Tree
- Two-Sum
- Find the non-duplicate number
- Generate All IP Addresses
- CPU Utilization
©2006~2024 牛排过熟 - 0.00952 秒 - 1931.574 KB/s - 31 在线 内存: 492.2 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)
读写完全 - 一般 - 最小 - 表格 - 所有评论 - 统计
当前页暂时没有评论。