File: root - text - computing - delphi - 2011 - 06 - tricks.txt
Tags: 计算机, Computing Tips, | English | Home Page | Category: Computing | 1010 Views, 29000 Search Bots | 142 Words
| Browse | Archive
Tags: 计算机, Computing Tips, | English | Home Page | Category: Computing | 1010 Views, 29000 Search Bots | 142 Words
| Browse | Archive
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)
Tags: 计算机, Computing Tips, | English | Home Page | Cateogry: Computing | 1010 Views, 29000 Search Bots | 142 Words 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)
Related Articles
- Non-decreasing Array with Single Modification
- Linode Support Ticket 10029540 - Other - Important Notice Regarding Ubuntu 17.10 Image
- [Daily Problem] Witness of The Tall People
- Maximum In A Stack
- Fibonacci coding
- Reverse a Directed Graph
- Binary Tree Level with Minimum Sum
- Consecutive Ones
- Skip the readings, focus on problems. And use all the hints!
- Daily Interview Problem: Reconstrunct Binary Tree from Preorder and Inorder Traversals
©2006~2024 SteakOverCooked - 0.00723 Seconds(s) - 2549.065 KB/s - 10 Online Memory: 493.61 KB
18:54:01 up 13 days, 18:33, 2 users, load average: 0.98, 0.86, 0.73 - Server PHP Version: 7.4.33
How to Cook a Perfect Steak? | <meta name="robots" content="index, follow">
18:54:01 up 13 days, 18:33, 2 users, load average: 0.98, 0.86, 0.73 - Server PHP Version: 7.4.33
Comments (0)
Read & Write - Normal - Mini - Post - All Comments - Statistics
Be the first one to comment this page !