Efficiency

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
SidewinderX
Forum Contributor
Posts: 407
Joined: Fri Jul 16, 2004 9:04 pm
Location: NY

Efficiency

Post by SidewinderX »

I have a script that i just threw together that does the job, but it takes literally 15 minutes to finish loading and im sure it can be much more efficient.

Its an "engine" that grabs an ID from the database, connects to a URL, parses stats from that URL and then updates the database with the new stats. However half the time the script times out, and when it dosnt it just takes a long time to load. As more users are added to the database it will become even worse as it loops for every ID in the database.

Can someone please rip it to shreads?

Code: Select all

<?php
include("config.php");
$result40 = mysql_query("SELECT * FROM " . $dbtable , $db);
while ($row40 = mysql_fetch_assoc($result40))
{

$pid = $row40['pid'];


//Gets the content of the submited URL;
$url = "https://www.novaworld.com/Players/Stats.aspx?id=".$row40['pid']."&p=616065";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_POST, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); //use ssl connection
curl_setopt($ch, CURLOPT_HTTPPROXYTUNNEL, 1);
curl_setopt($ch, CURLOPT_PROXY, '80.249.72.180:80');  //use proxy
$content = curl_exec($ch);
curl_close ($ch);

//Strips the tags of the $content;
$strip = strip_tags($content);

//Removes Everything in the up to For:;
$remove = stristr($strip, 'Hawk Down');

//Declares $remove as $getstats;
$stats = $remove;

//Player Name
preg_match('#Down\s+(.*?)\s+Rank#', $strip, $matches);
$player_name = $matches[1];

###################
### Start to parse stats ###
###################
//Player Rank
preg_match('#\( (.*?) \)#', $stats, $matches);
$rank = $matches[1];
if($rank == "1"){
$player_rank = "Private"; //Player is a Private
}
elseif($rank == "2"){
$player_rank = "Private First Class"; //Player is a Private First Class
}
elseif($rank == "3"){
$player_rank = "Corporal"; //Player is a Corporal
}
elseif($rank == "4"){
$player_rank = "Sergeant"; //Player is a Sergeant
}
elseif($rank == "5"){
$player_rank = "Sergeant First Class"; //Player is a Sergent First Class
}
elseif($rank == "6"){
$player_rank = "Master Sergeant"; //Player is a Master Sergeant
}
elseif($rank == "7"){
$player_rank = "Sergeant Major"; //Player is a Sergeant Major
}
elseif($rank == "8"){
$player_rank = "Second Lieutenant"; //Player is a Second Lieutenant
}
elseif($rank == "9"){
$player_rank = "First Lieutenant"; //Player is a First Lieutenant
}
elseif($rank == "10"){
$player_rank = "Captain"; //Player is a Captian
}
elseif($rank == "11"){
$player_rank = "Major"; //Player is a Major
}
elseif($rank == "12"){
$player_rank = "Lieutenant Colonel"; //Player is a Lieutenant Colonel
}
elseif($rank == "13"){
$player_rank = "Colonel"; //Player is a Colonel
}
elseif($rank == "14"){
$player_rank = "1-Star General"; //Player is a 1-Star General
}
elseif($rank == "15"){
$player_rank = "2-Star General"; //Player is a 2-Star General
}
elseif($rank == "16"){
$player_rank = "3-Star General"; //Player is a 3-Star General
}
elseif($rank == "17"){
$player_rank = "4-Star General"; //Player is a 4-Star General
}
elseif($rank == "18"){
$player_rank = "5-Star General"; //Player is a 5-Star General
}
else{
$player_rank = "Rank Error";
}

//PCID;
$pcid = stristr($stats, 'PCID:');
sscanf($pcid, 'PCID: %s' , $pcid);

//Total Team Games Played;
$total_team_games_played = stristr($stats, 'Total Team Games Played:');
sscanf($total_team_games_played, 'Total Team Games Played: %s' , $total_team_games_played);

//Total Kills;
$total_kills = stristr($stats, 'Total Kills:');
sscanf($total_kills, 'Total Kills: %s' , $total_kills);

//Team Win Percentage:
$team_win_percentage = stristr($stats, 'Team Win Percentage:');
sscanf($team_win_percentage, 'Team Win Percentage: %s' , $team_win_percentage);

//Deahs:
$deaths = stristr($stats, 'Deaths:');
sscanf($deaths, 'Deaths: %s' , $deaths);

//Killed 2 for 1:
$killed_2_for_1 = stristr($stats, 'Killed 2 for 1:');
sscanf($killed_2_for_1, 'Killed 2 for 1: %s' , $killed_2_for_1);

//Login Count:
$login_count = stristr($stats, 'Login Count:');
sscanf($login_count, 'Login Count: %s' , $login_count);

//Medical Saves:
$medical_saves = stristr($stats, 'Medical Saves:');
sscanf($medical_saves, 'Medical Saves: %s' , $medical_saves);

//Minutes Played:
$minutes_played = stristr($stats, 'Minutes Played:');
sscanf($minutes_played, 'Minutes Played: %s' , $minutes_played);

//PSP Takeover Attempts:
$psp_takeover_attempts = stristr($stats, 'PSP Takeover Attempts:');
sscanf($psp_takeover_attempts, 'PSP Takeover Attempts: %s' , $psp_takeover_attempts);

//PSP Takeovers:
$psp_takeovers = stristr($stats, 'PSP Takeovers:');
sscanf($psp_takeovers, 'PSP Takeovers: %s' , $psp_takeovers);

//Saved by Medics:
$saved_by_medics = stristr($stats, 'Saved by Medics:');
sscanf($saved_by_medics, 'Saved by Medics: %s' , $saved_by_medics);

//Team Draws:
$team_draws = stristr($stats, 'Team Draws:');
sscanf($team_draws, 'Team Draws: %s' , $team_draws);

//Team Experience Points:
$team_experience_points = stristr($stats, 'Team Experience Points:');
sscanf($team_experience_points, 'Team Experience Points: %s' , $team_experience_points);

//Team Losses:
$team_losses = stristr($stats, 'Team Losses:');
sscanf($team_losses, 'Team Losses: %s' , $team_losses);

//Team Wins
$team_wins = stristr($stats, 'Team Wins:');
sscanf($team_wins, 'Team Wins: %s' , $team_wins);

//Total Head Shots:
$total_head_shots = stristr($stats, 'Total Head Shots:');
sscanf($total_head_shots, 'Total Head Shots: %s' , $total_head_shots);

//Assault Rifle:
$remove2 = stristr($strip, 'Accuracy');
$stats2 = $remove2;
$assault_rifle = stristr($stats2, 'Assault Rifle');
sscanf($assault_rifle, 'Assault Rifle %s %s %s %s %s' , $assault_rifle_uses, $assault_rifle_hits, $assault_rifle_kills, $assault_rifle_headshots, $assault_rifle_accuracy);

//Claymores
$claymores = stristr($stats2, 'Claymores');
sscanf($claymores, 'Claymores %s %s %s %s %s' , $claymore_uses, $claymore_hits, $claymore_kills, $claymore_headshots, $claymore_accuracy);

//Emplaced Weapons
$emplaced_weapons = stristr($stats2, 'Emplaced Weapons');
sscanf($emplaced_weapons, 'Emplaced Weapons %s %s %s %s %s' , $emplaced_weapons_uses, $emplaced_weapons_hits, $emplaced_weapons_kills, $emplaced_weapons_headshots, $emplaced_weapons_accuracy);

//Grenades
$grenades = stristr($stats2, 'Grenades');
sscanf($grenades, 'Grenades %s %s %s %s %s' , $grenades_uses, $grenades_hits, $grenades_kills, $grenades_headshots, $grenades_accuracy);

//Knife
$knife = stristr($stats2, 'Knife');
sscanf($knife, 'Knife %s %s %s %s %s' , $knife_uses, $knife_hits, $knife_kills, $knife_headshots, $knife_accuracy);

//Machine Gun
$machine_gun = stristr($stats2, 'Machine Gun');
sscanf($machine_gun, 'Machine Gun %s %s %s %s %s' , $machine_gun_uses, $machine_gun_hits, $machine_gun_kills, $machine_gun_headshots, $machine_gun_accuracy);

//Missiles
$missiles = stristr($stats2, 'Missiles');
sscanf($missiles, 'Missiles %s %s %s %s %s' , $missiles_uses, $missiles_hits, $missiles_kills, $missiles_headshots, $missiles_accuracy);

//Pistol
$pistol = stristr($stats2, 'Pistol');
sscanf($pistol, 'Pistol %s %s %s %s %s' , $pistol_uses, $pistol_hits, $pistol_kills, $pistol_headshots, $pistol_accuracy);

//Satchel
$satchel = stristr($stats2, 'Satchel');
sscanf($satchel, 'Satchel %s %s %s %s %s' , $satchel_uses, $satchel_hits, $satchel_kills, $satchel_headshots, $satchel_accuracy);

//Shotgun
$shotgun = stristr($stats2, 'Shotgun');
sscanf($shotgun, 'Shotgun %s %s %s %s %s' , $shotgun_uses, $shotgun_hits, $shotgun_kills, $shotgun_headshots, $shotgun_accuracy);

//Sniper Rifle
$sniper_rifle = stristr($stats2, 'Sniper Rifle');
sscanf($sniper_rifle, 'Sniper Rifle %s %s %s %s %s' , $sniper_rifle_uses, $sniper_rifle_hits, $sniper_rifle_kills, $sniper_rifle_headshots, $sniper_rifle_accuracy);

//Submachine Gun
$submachine_gun = stristr($stats2, 'Submachine Gun');
sscanf($submachine_gun, 'Submachine Gun %s %s %s %s %s' , $submachine_gun_uses, $submachine_gun_hits, $submachine_gun_kills, $submachine_gun_headshots, $submachine_gun_accuracy);

$deathmatch = stristr($strip, 'Deathmatch Games');

//Deathmatch Levels
$dm_levels = stristr($deathmatch, 'Level:');
sscanf($dm_levels, 'Level: %s' , $dm_levels);

//Deathmatch Deaths
$dm_deaths = stristr($deathmatch, 'Deaths:');
sscanf($dm_deaths, 'Deaths: %s' , $dm_deaths);

//Deathmatch Doubles
$dm_doubles = stristr($deathmatch, 'Killed 2 for 1:');
sscanf($dm_doubles, 'Killed 2 for 1: %s' , $dm_doubles);

//Deathmatch Medical Saves:
$dm_medsaves = stristr($deathmatch, 'Medical Saves:');
sscanf($dm_medsaves, 'Medical Saves: %s' , $dm_medsaves);

//Deathmatch Minutes Played
$dm_minplayed = stristr($deathmatch, 'Minutes Played:');
sscanf($dm_minplayed, 'Minutes Played: %s' , $dm_minplayed);

//Deathmatch Team Experience Points:
$dm_experiencepoints = stristr($deathmatch, 'Team Experience Points:');
sscanf($dm_experiencepoints, 'Team Experience Points: %s' , $dm_experiencepoints);

//Deathmatch Total Head Shots:
$dm_headshots = stristr($deathmatch, 'Total Head Shots:');
sscanf($dm_headshots, 'Total Head Shots: %s' , $dm_headshots);

//Deathmatch Total Head Shots:
$dm_totalkills = stristr($deathmatch, 'Total Kills:');
sscanf($dm_totalkills, 'Total Kills: %s' , $dm_totalkills);

$koth = stristr($strip, 'King of the Hill Games');

//KOTH Levels:
$koth_levels = stristr($koth, 'Level:');
sscanf($koth_levels, 'Level: %s' , $koth_levels);

//KOTH Deaths:
$koth_deaths = stristr($koth, 'Deaths:');
sscanf($koth_deaths, 'Deaths: %s' , $koth_deaths);

//KOTH Doubles:
$koth_doubles = stristr($koth, 'Killed 2 for 1:');
sscanf($koth_doubles, 'Killed 2 for 1: %s' , $koth_doubles);

//KOTH Medical Saves:
$koth_medicalsaves = stristr($koth, 'Medical Saves:');
sscanf($koth_medicalsaves, 'Medical Saves: %s' , $koth_medicalsaves);

//KOTH Minutes in KOTH Zone:
$koth_zonetime = stristr($koth, 'Minutes in KOTH Zone:');
sscanf($koth_zonetime, 'Minutes in KOTH Zone: %s' , $koth_zonetime);

//KOTH Minutes Played:
$koth_minutesplayed = stristr($koth, 'Minutes Played:');
sscanf($koth_minutesplayed, 'Minutes Played: %s' , $koth_minutesplayed);

//KOTH Team Experience Points:
$koth_experiencepoints = stristr($koth, 'Team Experience Points:');
sscanf($koth_experiencepoints, 'Team Experience Points: %s' , $koth_experiencepoints);

//KOTH Total Head Shots:
$koth_headshots = stristr($koth, 'Total Head Shots:');
sscanf($koth_headshots, 'Total Head Shots: %s' , $koth_headshots);

//KOTH Total Kills:
$koth_totalkills = stristr($koth, 'Total Kills:');
sscanf($koth_totalkills, 'Total Kills: %s' , $koth_totalkills);

$flags = stristr($strip, 'Flag Games');

//Flag Levels:
$flag_levels = stristr($flags, 'Level:');
sscanf($flag_levels, 'Level: %s' , $flag_levels);

//Flag Flags Captured:
$flag_captured = stristr($flags, 'Flags Captured:');
sscanf($flag_captured, 'Flags Captured: %s' , $flag_captured);

//Flag Flags Saved:
$flag_saved = stristr($flags, 'Flags Saved:');
sscanf($flag_saved, 'Flags Saved: %s' , $flag_saved);

//Flag Deaths:
$flag_deaths = stristr($flags, 'Deaths:');
sscanf($flag_deaths, 'Deaths: %s' , $flag_deaths);

//Flag Killed 2 for 1:
$flag_doubles = stristr($flags, 'Killed 2 for 1:');
sscanf($flag_doubles, 'Killed 2 for 1: %s' , $flag_doubles);

//Flag Medical Saves:
$flag_medsaves = stristr($flags, 'Medical Saves:');
sscanf($flag_medsaves, 'Medical Saves: %s' , $flag_medsaves);

//Flag Minutes Played:
$flag_minutesplayed = stristr($flags, 'Minutes Played:');
sscanf($flag_minutesplayed, 'Minutes Played: %s' , $flag_minutesplayed);

//Flag Team Experience Points:
$flag_experiencepoints = stristr($flags, 'Team Experience Points:');
sscanf($flag_experiencepoints, 'Team Experience Points: %s' , $flag_experiencepoints);

//Flag Total Head Shots:
$flag_headshots = stristr($flags, 'Total Head Shots:');
sscanf($flag_headshots, 'Total Head Shots: %s' , $flag_headshots);

//Flag Total Kills:
$flag_totalkills = stristr($flags, 'Total Kills:');
sscanf($flag_totalkills, 'Total Kills: %s' , $flag_totalkills);

$basewar = stristr($strip, 'Base War Games');

//Base War Levels:
$bw_levels = stristr($basewar, 'Level:');
sscanf($bw_levels, 'Level: %s' , $bw_levels);

//Base War Base Targets Destroyed:
$bw_targets = stristr($basewar, 'Base Targets Destroyed:');
sscanf($bw_targets, 'Base Targets Destroyed: %s' , $bw_targets);

//Base War Deaths:
$bw_deaths = stristr($basewar, 'Deaths:');
sscanf($bw_deaths, 'Deaths: %s' , $bw_deaths);

//Base War Killed 2 for 1:
$bw_doubles = stristr($basewar, 'Killed 2 for 1:');
sscanf($bw_doubles, 'Killed 2 for 1: %s' , $bw_doubles);

//Base War Medical Saves:
$bw_medicalsaves = stristr($basewar, 'Medical Saves:');
sscanf($bw_medicalsaves, 'Medical Saves: %s' , $bw_medicalsaves);

//Base War Minutes Played:
$bw_minutesplayed = stristr($basewar, 'Minutes Played:');
sscanf($bw_minutesplayed, 'Minutes Played: %s' , $bw_minutesplayed);

//Base War Team Experience Points:
$bw_experiencepoints = stristr($basewar, 'Team Experience Points:');
sscanf($bw_experiencepoints, 'Team Experience Points: %s' , $bw_experiencepoints);

//Base War Total Head Shots:
$bw_headshots = stristr($basewar, 'Total Head Shots:');
sscanf($bw_headshots, 'Total Head Shots: %s' , $bw_headshots);

//Base War Kills:
$bw_kills = stristr($basewar, 'Kills:');
sscanf($bw_kills, 'Kills: %s' , $bw_kills);

$kill_death_ratio = $total_kills / $deaths;
$kill_death_ratio = substr("$kill_death_ratio", 0, 5);
$win_loss_ratio = $total_team_games_played / $team_losses;
$win_loss_ratio = substr("$win_loss_ratio", 0, 5);
$head_shot_ratio = $total_head_shots / $total_kills;
$head_shot_ratio = substr("$head_shot_ratio", 0, 5);
$total_time_palyed = $bw_minutesplayed + $koth_minutesplayed + $flag_minutesplayed + $dm_minplayed;
$total_levels = $bw_levels + $dm_levels + $flag_levels + $koth_levels;
$kpm = $total_kills / $total_time_palyed;
$dpm = $deaths / $total_time_palyed;
$average_exp = $team_experience_points / $total_team_games_played;
#################
### End Stat parsing ###
#################


####################
### Update the database ###
####################
$sql = mysql_query("UPDATE ".$dbtable." SET player_name='$player_name', player_rank='$player_rank', pcid='$pcid', total_team_games_played='$total_team_games_played', total_time_palyed='$total_time_palyed', total_kills='$total_kills', team_win_percentage='$team_win_percentage', deaths='$deaths', killed_2_for_1='$killed_2_for_1', login_count='$login_count', medical_saves='$medical_saves', minutes_played='$minutes_played', psp_takeover_attempts='$psp_takeover_attempts', psp_takeovers='$psp_takeovers', saved_by_medics='$saved_by_medics', team_draws='$team_draws', team_experience_points='$team_experience_points', team_losses='$team_losses', team_wins='$team_wins', total_head_shots='$total_head_shots', assault_rifle_uses='$assault_rifle_uses', assault_rifle_hits='$assault_rifle_hits', assault_rifle_kills='$assault_rifle_kills', assault_rifle_headshots='$assault_rifle_headshots', assault_rifle_accuracy='$assault_rifle_accuracy', claymore_uses='$claymore_uses', claymore_hits='$claymore_hits', claymore_kills='$claymore_kills', claymore_headshots='$claymore_headshots', claymore_accuracy='$claymore_accuracy', emplaced_weapons_uses='$emplaced_weapons_uses', emplaced_weapons_hits='$emplaced_weapons_hits', emplaced_weapons_kills='$emplaced_weapons_kills', emplaced_weapons_headshots='$emplaced_weapons_headshots', emplaced_weapons_accuracy='$emplaced_weapons_accuracy', grenades_uses='$grenades_uses', grenades_hits='$grenades_hits', grenades_kills='$grenades_kills', grenades_headshots='$grenades_headshots', grenades_accuracy='$grenades_accuracy', knife_uses='$knife_uses', knife_hits='$knife_hits', knife_kills='$knife_kills', knife_headshots='$knife_headshots', knife_accuracy='$knife_accuracy', machine_gun_uses='$machine_gun_uses', machine_gun_hits='$machine_gun_hits', machine_gun_kills='$machine_gun_kills', machine_gun_headshots='$machine_gun_headshots', machine_gun_accuracy='$machine_gun_accuracy', missile_uses='$missiles_uses', missile_hits='$missiles_hits', missile_kills='$missiles_kills', missile_headshots='$missiles_headshots', missile_accuracy='$missiles_accuracy', pistol_uses='$pistol_uses', pistol_hits='$pistol_hits', pistol_kills='$pistol_kills', pistol_headshots='$pistol_headshots', pistol_accuracy='$pistol_accuracy', satchel_uses='$satchel_uses', satchel_hits='$satchel_hits', satchel_kills='$satchel_kills', satchel_headshots='$satchel_headshots', satchel_accuracy='$satchel_accuracy', shotgun_uses='$shotgun_uses', shotgun_hits='$shotgun_hits', shotgun_kills='$shotgun_kills', shotgun_headshots='$shotgun_headshots', shotgun_accuracy='$shotgun_accuracy', sniper_rifle_uses='$sniper_rifle_uses', sniper_rifle_hits='$sniper_rifle_hits', sniper_rifle_kills='$sniper_rifle_kills', sniper_rifle_headshots='$sniper_rifle_headshots', sniper_rifle_accuracy='$sniper_rifle_accuracy', submachine_gun_uses='$submachine_gun_uses', submachine_gun_hits='$submachine_gun_hits', submachine_gun_kills='$submachine_gun_kills', submachine_gun_headshots='$submachine_gun_headshots', submachine_gun_accuracy='$submachine_gun_accuracy', dm_levels='$dm_levels', dm_deaths='$dm_deaths', dm_doubles='$dm_doubles', dm_medsaves='$dm_medsaves', dm_minplayed='$dm_minplayed', dm_experiencepoints='$dm_experiencepoints', dm_headshots='$dm_headshots', dm_totalkills='$dm_totalkills', koth_levels='$koth_levels', koth_deaths='$koth_deaths', koth_doubles='$koth_doubles', koth_medicalsaves='$koth_medicalsaves', koth_zonetime='$koth_zonetime', koth_minutesplayed='$koth_minutesplayed', koth_experiencepoints='$koth_experiencepoints', koth_headshots='$koth_headshots' , koth_totalkills='$koth_totalkills', flag_levels='$flag_levels', flag_captured='$flag_captured', flag_saved='$flag_saved', flag_deaths='$flag_deaths', flag_doubles='$flag_doubles', flag_medsaves='$flag_medsaves', flag_minutesplayed='$flag_minutesplayed', flag_experiencepoints='$flag_experiencepoints', flag_headshots='$flag_headshots', flag_totalkills='$flag_totalkills', bw_levels='$bw_levels', bw_targets='$bw_targets', bw_deaths='$bw_deaths', bw_doubles='$bw_doubles', bw_medicalsaves='$bw_medicalsaves', bw_minutesplayed='$bw_minutesplayed', bw_experiencepoints='$bw_experiencepoints', bw_headshots='$bw_headshots', bw_kills='$bw_kills', kill_death_ratio='$kill_death_ratio', win_loss_ratio='$win_loss_ratio', head_shot_ratio='$head_shot_ratio', total_levels='$total_levels', kpm='$kpm', dpm='$dpm', average_exp='$average_exp' WHERE pid = '$pid'") or die("Error: ".mysql_error());
} //end update
//ever see a query that large?
?>
printf
Forum Contributor
Posts: 173
Joined: Wed Jan 12, 2005 5:24 pm

Post by printf »

I would dump CURL and use non-blocking socket streams. So you can handle multi requests at one time! Like say you have 100 users, open 20 streams at one time and then keep adding a new stream when one finishes! This way your always processing at leat 20 requests at one time. With CURL 100 requests to google takes 22 seconds, using socket streams at 20 streams at a time takes less than 2 seconds! But I only recommend using streams if you have permission to access that content.


printf
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Post by alex.barylski »

OMG...you win the award for the longest SQL story...I mean SQL statement...I have ever personally seen :P
Post Reply