Optimization
Posted: Tue Oct 18, 2022 1:27 pm
Can you guys propose some types of optimizations to increase the performance of any program
Seconded. The fastest code is the code that doesn't run. Also the smallest.kzinti wrote:Sure... remove code. The less code you have the faster your program will be.
There is no silver bullet. Each program is different. My advice is to first build the program to solve your problem, then check to see if performance is sufficient for your (customer's) needs. If so, awesome, you are done.devc1 wrote:Can you guys propose some types of optimizations to increase the performance of any program
Old joke, paraphrased: All programs can be shortened by at least one instruction, and all programs contain at least one bug. Therefore, every program can be optimized down to one instruction which doesn't work.nullplan wrote:Seconded. The fastest code is the code that doesn't run. Also the smallest.kzinti wrote:Sure... remove code. The less code you have the faster your program will be.
That's obviously false.kzinti wrote:Sure... remove code. The less code you have the faster your program will be.
Remove the code to print the squares and it will be even faster.iansjack wrote:For example, write a program to print a table of squares from 1 to 10 - the fastest program will be much longer than the shortest one.
But then it won’t satisfy the condition of being a program to print a table of squares.kzinti wrote:Remove the code to print the squares and it will be even faster.iansjack wrote:For example, write a program to print a table of squares from 1 to 10 - the fastest program will be much longer than the shortest one.
That’s too general a question.devc1 wrote:How do we iterrate on a large array of linked lists faster ?
Indeed, I think some of the sarcasm in my initial reply got lost along the way. Re-read it in the context of the original question asking for a generic optimization solution to all programs. The only one I could come up with was to delete code. I was trying to be funny.iansjack wrote:It’s trivial to say that you can reduce the run time of any program to 0 by removing all the code. But it’s not very useful.
A lot of optimization is choosing the appropriate data structure. For example, if you frequently need to search a linked list for specific items, your best optimization might be replacing the linked list with a self-balancing binary tree.devc1 wrote:How do we iterrate on a large array of linked lists faster ?
Page tables are a type of tree data structure, but they're not a binary tree. (I don't actually know offhand what type of tree they are...)devc1 wrote:Do you mean by the binary tree some structures like page tables ?
If you're primarily using the list to look up the item using the ID, then you probably shouldn't create a linked list in the first place. If the data is static, you might create an array sorted by ID and use a binary search to find the item. If the data is dynamic, you might create a self-balancing binary tree.devc1 wrote:I guess the quicksort algorithm will be usefull when creating linked lists with an ID for each item, so you can use this algorithm to get the Item from the ID faster ?
Why do you need to search for both free and allocated items?devc1 wrote:I use an optimization to find free and allocated items is that on each linked list I create a bitmap where each bit represents an allocated entry/item.
You shouldn't worry about this kind of optimization until you have chosen an appropriate data structure. It sounds like a linked list is not the appropriate data structure.devc1 wrote:I use the bsf (Bit Scan Forward) to find an allocated item, to find a free item I just reverse the bits using the not instruction then bsf.