Alternative to json_encode

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
jraede
Forum Contributor
Posts: 254
Joined: Tue Feb 16, 2010 5:39 pm

Alternative to json_encode

Post by jraede »

I'm using json_encode to pass some database data to my google maps generator. The script takes about 200+ entries, gets their latitude, longitude, and name, and then sends that to javascript to make markers on the map. The script takes about .4 seconds to parse through each entry and push its info into the $results array, but takes a whopping 20+ seconds to run json_encode on that finished array.

Is there another way of outputting a json-encoded array? Or am I doing it wrong?

Here's the script:

Code: Select all

private function set_json_info() {
	$script_start = microtime_float();
	$city = get_table_data('cities', 'name', $this->display_metro);
	$area = get_table_data('regions', 'name', $this->display_area);
	$sub_area = get_table_data('sub_areas', 'name', $this->display_sub_area);
	if($this->display_sub_area) {
		$this->map_title = "$city - $area - $sub_area";
		$this->query_listings("object_class=venue&table=venues&area=".$this->display_area."&sub_area=".$this->display_sub_area);
	}
	elseif($this->display_area) {
		$this->map_title = "$city - $area";
		$this->query_listings("object_class=venue&table=venues&area=".$this->display_area);
	}
	else {
		$this->map_title = $city;
		$this->query_listings("object_class=venue&table=venues&metro=".$this->display_metro);
	}
	
	while($this->has_results()) {
		unset($result);
		$nf_listing = $this->create_object();
		$result['lat'] = $nf_listing->venue_latitude;
		$result['lng'] = $nf_listing->venue_longitude;
		$result['name'] = '<a href="'.$nf_listing->venue_url().'">'.$nf_listing->venue_name.'</a>';
		$address1 = $nf_listing->venue_address1;
		$address2 = $nf_listing->venue_address2;
		$city = $nf_listing->venue_city;
		$state = $nf_listing->venue_state;
		$zip = $nf_listing->venue_zip;
		if($address2) {
			$address2 = $address2."<br />";
		}
		
		$address = $address1."<br />".$address2.$city.", ".$state." ".$zip;
		$result['address'] = $address;
		$return[] = $result;
	}
	$script_end = microtime_float();
	$time = $script_end - $script_start;
	echo "Script took $time seconds to run."; // .4 seconds	
	
	$this->map_json_info = json_encode($return);
	
	$script_end = microtime_float();
	$time = $script_end - $script_start;
	echo "Script took $time seconds to run."; // 20+ seconds!!!
	
}
jraede
Forum Contributor
Posts: 254
Joined: Tue Feb 16, 2010 5:39 pm

Re: Alternative to json_encode

Post by jraede »

Anyone?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Alternative to json_encode

Post by Weirdan »

Just to make sure you're not mixing things up have you tried to display different strings in your profiling output? Like 'Data generation took X seconds' and 'Encoding took Y seconds'.
jraede
Forum Contributor
Posts: 254
Joined: Tue Feb 16, 2010 5:39 pm

Re: Alternative to json_encode

Post by jraede »

I mean, it displays both strings, so at the top of the page it says "Script took .4 seconds to runScript took 25.45 seconds to run." Unless for some weird reason the php gets parsed in reverse order, I'm pretty sure that it's the json_encode that's taking so long.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Alternative to json_encode

Post by requinix »

About how large is $return? Or alternatively, how large is the JSON result?
jraede
Forum Contributor
Posts: 254
Joined: Tue Feb 16, 2010 5:39 pm

Re: Alternative to json_encode

Post by jraede »

152 venues.
Post Reply