How to optimize this algorithm?

Filed under:
|
|
|
optimization

I have two sets of arrays like this for example.

``````\$Arr1['uid'][]='user 1'; \$Arr1['weight'][]=1;
\$Arr1['uid'][]='user 2'; \$Arr1['weight'][]=10;
\$Arr1['uid'][]='user 3'; \$Arr1['weight'][]=5;

\$Arr2['uid'][]='user 1'; \$Arr2['weight'][]=3;
\$Arr2['uid'][]='user 4'; \$Arr2['weight'][]=20;
\$Arr2['uid'][]='user 5'; \$Arr2['weight'][]=15;
\$Arr2['uid'][]='user 2'; \$Arr2['weight'][]=2;
``````

The size of two arrays could be different of course. \$Arr1 has coefficient of 0.7 and \$Arr2 has coefficient of 0.3. I need to calculate following formula

``````\$result=\$Arr1['weight'][\$index]*\$Arr1Coeff+\$Arr2['weight'][\$index]*\$Arr2Coeff;
``````

where `\$Arr1['uid']=\$Arr2['uid']`. So when `\$Arr1['uid']` doesn't exists in `\$Arr2` then we need to omit `\$Arr2` and vice versa.
And, here is an algorithm I am using now.

``````foreach(\$Arr1['uid'] as \$index=>\$arr1_uid){
\$pos=array_search(\$arr1_uid, \$Arr2['uid']);
if (\$pos===false){
\$result=\$Arr1['weight'][\$index]*\$Arr1Coeff;
echo "<br>\$arr1_uid has not found and RES=".\$result;
}else{
\$result=\$Arr1['weight'][\$index]*\$Arr1Coeff+\$Arr2['weight'][\$pos]*\$Arr2Coeff;
echo "<br>\$arr1_uid has found on \$pos and RES=".\$result;
}
}

foreach(\$Arr2['uid'] as \$index=>\$arr2_uid){
if (!in_array(\$arr2_uid, \$Arr1['uid'])){
\$result=\$Arr2['weight'][\$index]*\$Arr2Coeff;
echo "<br>\$arr2_uid has not found and RES=".\$result;
}else{
echo "<br>\$arr2_uid has found somewhere";
}
}
``````

The question is how can I optimize this algorithm? Can you offer other better solution for this problem?
Thank you.

© Stack Overflow or respective owner

Related posts about php

• Magento, NGINX, PHP-FPM, APC, MEMCACHED, 16gb Ram CentOS, Spiking PHP-FPM to 100% CPU

as seen on Server Fault - Search for 'Server Fault'
I have been trying to resolve my issue of spiking cpu caused by php-fpm processes. I've reduced the php-fpm config settings to: pm = ondemand pm.max_children = 12 pm.start_servers = 2 pm.min_spare_servers = 2 pm.max_spare_servers = 10 pm.max_requests = 500 php_admin_value[memory_limit] = 128M Problem… >>> More

• PHP Pear Installation on CentOS

as seen on Server Fault - Search for 'Server Fault'
[root@ip ~]# yum install php-pear* Reducing CentOS-5 Testing to included packages only Finished Setting up Install Process Package 1:php-pear-1.8.1-2.el5.centos.noarch already installed and latest versio … >>> More

• Apache configurations for php "AddType text/html php" or "AddType application/x-httpd-php php .php"

as seen on Server Fault - Search for 'Server Fault'
I am taking over an application server and discover that it contain the following settings: AddType text/html php Although it works, but my understanding is that it should set as following: AddType application/x-httpd-php php .php What are the key differences between the two settings?… >>> More

• mod_rewrite settings causes server to throw HTTP 500 errors instead of 404

as seen on Server Fault - Search for 'Server Fault'
Hello. I have a server with VBulletin forum (working under Apache 2.2, CentOS). The default settings for it in .htaccess are as follows: RewriteEngine on RewriteCond %{HTTP_HOST} ^gsmforum\.ru RewriteRule (.*) http://www.gsmforum.ru/\$1 [R=301,L] # If you are having problems or are using VirtualDocumentRoot… >>> More

• Problems installing Memcache (PECL extension)

as seen on Server Fault - Search for 'Server Fault'
I have installed memcached fine, and now I will need to install PECL extension memcache. Im running RedHat x86_64 es5. The installation gives me this: downloading memcache-2.2.6.tgz ... Starting to download memcache-2.2.6.tgz (35,957 bytes) ..........done: 35,957 bytes 11 source files, building running:… >>> More

Related posts about algorithm

• Jpeg Algorithm vs BMP Algorithm?

as seen on Super User - Search for 'Super User'
I'm just wonder, what the differences are between creating a BMP file algorithm and JPG file algorithm ? If you know the others images' format algorithm, please post them. Thanks. >>> More

• word disambiguation algorithm (Lesk algorithm)

as seen on Stack Overflow - Search for 'Stack Overflow'
Hii.. Can anybody help me to find an algorithm in Java code to find synonyms of a search word based on the context and I want to implement the algorithm with WordNet database. For example, "I am running a Java program". From the context, I want to find the synonyms for the word "running", but the… >>> More

• Search algorithm (with a sort algorithm already implemented)

as seen on Stack Overflow - Search for 'Stack Overflow'
Hello, Im doing a Java application and Im facing some doubts in which concerns performance. I have a PriorityQueue which guarantees me the element removed is the one with greater priority. That PriorityQueue has instances of class Event (which implements Comparable interface). Each Event is associated… >>> More

• Is there any algorithm for finding LINES by PIXEL COLORS on picture?

as seen on Stack Overflow - Search for 'Stack Overflow'
So I have Image like this I want to get something like this (I hevent drawn all lines I want but I hope you can get my idea) I need algorithm for finding all straight lines on it by just reading colors of pixels. No hard math, no Haar, no Hough. Some algorithm which would be based on points… >>> More

• collsion issues with quadtree [on hold]

as seen on Game Development - Search for 'Game Development'
So i implemented a Quad tree in Java for my 2D game and everything works fine except for when i run my collision detection algorithm, which checks if a object has hit another object and which side it hit.My problem is 80% of the time the collision algorithm works but sometimes the objects just go… >>> More