Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Database, or something else?
#1
For a php-script, would it be faster to load another .php instead of loading data from a database? Data doesn't have to be secured, it just have to be loaded fast. It's going to be a double-"array" like this:

Code:
$lst = new Array("New York" => new Array("Los Angeles" => 3944, "Jacksonville" => 1362),
          "Los Angeles" => new Array("New York" => 3944, "Jacksonville" => 3463),
          "Jacksonville" => new Array("New York" => 1362, "Los Angeles" => 3463));

 Spoiler

What would be the best is to have this data accessible at any given time, i.e. it's saved within the memory at any given time. Any way to do that?

Noah
Reply
#2
No need for a database - that just creates additional complexity with getting variables from the database and setting up the connection to the database from PHP and inserting the values in PHP. Plus, if your implementation changes this increases the complexity. Take out the database and use json_decode() or parse_ini_file() to get direct access to variables without a whole lot of hassle. The ini file is accessed and loaded directly into objects for you - much easier than using getter/setter methods to retrieve variables from a database, and then there's no need to configure the database from scratch.

Now, if you're talking THOUSANDS of nodes for Dijkstra's algorithm, then a database might be better for you. But until you reach that point (and that amount of processing power), a simple INI file will do the trick.

http://php.net/manual/en/function.parse-ini-file.php

Or, if you're looking for something a bit more homebrew, you could try building your own variation. Like so:

Code:
//0: Node Number
//1: Name of City
//2. (x, y) coordinates
//3. [Nodes connected]

//Final line
//Source Node -> Dest Node

0 New York (1300, 1200) [1, 2]
1 Chicago (1200, 1200) [1, 3]
2 Dallas (300, 1000) [0, 1]
3 Trenton (200, 500) [1, 2, 3]

0 -> 3

Then, if you need the length of one node to another, generate it dynamically:

d=Sqrt[(x_a-x_b)^2+(y_a-y_b)^2]

Of course this goes against Dijkstra's first premise, but I'm sure you can work around that.

This homebrew file would be fairly easy to parse with regex and would be simple to add/remove nodes.
Reply
#3
I would think that storing the variables in a .php file would be faster. But does it even matter which is faster? Do whatever's simpler, more readable, and generally best in other ways before worrying about speed, THEN optimize if needed. Don't optimize without measuring.

If all you need is to load the entire graph and not doing any searching, sorting, querying, etc, then I think a database would be overkill.

The rules of Optimization Club:

1. The first rule of Optimization Club is, you do not Optimize.
2. The second rule of Optimization Club is, you do not Optimize without measuring.
3. If your app is running faster than the underlying transport protocol, the optimization is over.

4. One factor at a time.
5. No marketroids, no marketroid schedules.
6. Testing will go on as long as it has to.
7. If this is your first night at Optimization Club, you have to write a test case.
Reply
#4
That's indeed a good point Spaz.

The thing is, I'm afraid it actually will take some time to load all the nodes, as the amount of nodes are around 3500-4000. Maybe not a major bottleneck compared to Dijkstra and sending the information back, but for me, it seems like there shouldn't be a big problem finding out how to load it as fast as possible. I mean, someone must have done something like this before, right?

Maybe I should be more specific and ask the following: Is it possible to add in a constant/variable within php which is accessible from everywhere without the need to load (or to "load", but that it is already loaded within memory)?

Noah
Reply
#5
Do you mean basically having some memory persist between web requests without the need to load the graph for every web request? If so, sorry, I don't know. That would indeed probably be a legitimate performance concern. One way you could do that without using some special php/web server feature would be to create a separate program that loads the data then listens for requests using some form of IPC (perhaps Unix domain sockets if on a Unix system) and responds to those requests. Make sure the program is always running when the web server is.

If there's an actual PHP way to do that, that would probably be preferable.
Reply
#6
skip that and load from mysql
Reply


Forum Jump:


Users browsing this thread: