2

I'm a newbie to Smarty Template Engine. I'm comparing two arrays. Following is my code from smarty template:

{foreach from=$all_states item=state key=key}
  {assign var="flag" value=false}
  {foreach from=$data.applicable_states item=pre key='index'}
    {if $state.id == $pre } {assign var="flag" value=true} {$break} {/if}
  {/foreach}
  <option value="{$state.id}" {if $flag == true} selected="selected"{/if}>{$state.state_name}</option>      
{/foreach}

The above code is working fine for me. But if you observe closely the no. of iterations performed to compare array values are too large in number.

So, I want to optimize the above code in order to reduce the number of iterations. Can someone who has good command over Smarty can provide some way to optimize the above code please?

halfer
  • 19,824
  • 17
  • 99
  • 186
PHPLover
  • 1
  • 51
  • 158
  • 311

4 Answers4

0

Maybe you can use in_array

{foreach from=$all_states item=state key=key}    
   <option value="{$state.id}" {if $state.id|in_array:$data.applicable_states} selected="selected"{/if}>{$state.state_name}</option>
{/foreach}
Borgtex
  • 3,235
  • 1
  • 19
  • 28
0

Actually the are some good algorithms out there that you could use. Each have complexity based on the number of comparisons it does so if you want to improve your code and use less number of comparisons, making this faster at the same time you could take a look to this list:

  • QuickSort O(nlog(n))
  • Bubblesort O(n^2)
  • cocktailsort ( bubble with some modifications) O(n^2)
  • Selection sort O(n^2)
  • MergeSort O(nlog(n))
  • HeapSOrt O(nlog(n))
  • InsertionSort O(n^2)

The one you are using is named "BubbleSort", so what is n ? N is the number of elements in your arrays.. and the O results is the amount of comparisons you have to do to get the collections order. So the better complexity is in the order of log. So algorithms like quicksort, merge, heap, are pretty cool if you want good performance, in my opinion the better in this list is the quicksort.

So how quicksort works? Quicksort is based on the principle "divide and conquer", using recursion. ( As you know a recursion solution uses more memory than an normal solution )

This is how QuickSort algorithm.

  1. The first is to choose a pivot element. This could be in the middle, left o right of the array.
  2. Compare each element of the list with the pivot element and put the elements to right ( if they are higher than pivot ) or to the left ( otherwise condition ).
  3. As you know, now you have 2 lists, one at the left and other in the right. so all you have to do is call again the QuickSort function recursively over those 2 lists while its lists have more than one elements.

Here is more information about this algorithm

You should know that quicksort is not the best, mixing sort algorithm and using it in different range could improve you code to, but this quicksort itself is good enough. ( I know you dont want to sort, so you need to modify to algorithm to do you work instead of sorting, but still is the one that made less comparisons, hope this could help you )

rahpuser
  • 1,224
  • 10
  • 31
0

You can implement other algorithm http://www.sorting-algorithms.com/ reducing the number of iterations in your algorithm.

All depend of the proposal of your algorithm, just now, you are using the buble sort method for 'N' numbers of elements, and obtain a 'X' result. The simple buble sort is good for short array, but for big arrays you can use: impact algorithms, like:

Knowing which sort algorithm performs better really depend on your data and situation.

If you are talking in general/practical terms,

  • Quicksort (the one where you select the pivot randomly/just pick one fixed, making worst case Omega(n^2)) might be better than Red-Black Trees because (not necessarily in order of importance)
  • Quicksort is in-place. The keeps your memory footprint low. Say this quicksort routine was part of a program which deals with a lot of data. If you kept using large amounts of memory, your OS could start swapping your process memory and trash your perf.
  • Quicksort memory accesses are localized. This plays well with the caching/swapping.
  • Quicksort can be easily parallelized (probably more relevant these days).
  • If you were to try and optimize binary tree sorting (using binary tree without balancing) by using an array instead, you will end up doing something like Quicksort!
  • Red-Black trees have memory overheads. You have to allocate nodes possibly multiple times, your memory requirements with trees is doubles/triple that using arrays.
  • After sorting, say you wanted the 1045th (say) element, you will need to maintain order statistics in your tree (extra memory cost because of this) and you will have O(logn) access time! Red-black trees have overheads just to access the next element (pointer lookups) Red-black trees do not play well with the cache and the pointer accesses could induce more swapping.
  • Rotation in red-black trees will increase the constant factor in the O(nlogn).

Perhaps the most important reason (but not valid if you have lib etc available), Quicksort is very simple to understand and implement. Even a school kid can understand it!

Community
  • 1
  • 1
Benjamin RD
  • 11,516
  • 14
  • 87
  • 157
0

try this

{$tmp = []}
{foreach from=$data.applicable_states item=pre key='index'}
    {assign var="$tmp[$pre]" value=1}
{/foreach}


{foreach from=$all_states item=state key=key}
    <option value="{$state.id}" {if isset($tmp[$state.id])} selected="selected"{/if}>
    {$state.state_name}</option>      
{/foreach}
Hieu Vo
  • 3,105
  • 30
  • 31