Page 1 of 1

Alternative to json_encode

Posted: Sat May 08, 2010 2:24 am
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!!!
	
}

Re: Alternative to json_encode

Posted: Sun May 09, 2010 2:09 pm
by jraede
Anyone?

Re: Alternative to json_encode

Posted: Sun May 09, 2010 2:46 pm
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'.

Re: Alternative to json_encode

Posted: Sun May 09, 2010 2:52 pm
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.

Re: Alternative to json_encode

Posted: Mon May 10, 2010 1:03 am
by requinix
About how large is $return? Or alternatively, how large is the JSON result?

Re: Alternative to json_encode

Posted: Mon May 10, 2010 1:07 am
by jraede
152 venues.