File: root - text - computing - delphi - 2011 - 06 - tricks.txt
Tags: 计算机, Computing Tips, | English | Home Page | Category: Computing | 1010 Views, 28999 Search Bots | 142 Words
| Browse | Archive
Tags: 计算机, Computing Tips, | English | Home Page | Category: Computing | 1010 Views, 28999 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, 28999 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
- Spectrum Master
- Daily Interview Question: Find Cycles in a Graph
- [Daily Problem] Witness of The Tall People
- Sorting a list with 3 unique numbers
- Absolute Path
- Daily Interview Problem: Deepest Node in a Binary Tree
- Daily Interview Problem: Min Stack
- Spreadsheet Column Title
- Daily Interview Puzzle: Ways to Traverse a Grid
- Algorithm Interview: Smallest Number that is not a Sum of a Subset of List
©2006~2024 SteakOverCooked - 0.00694 Seconds(s) - 2659.913 KB/s - 9 Online Memory: 493.09 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 !