l3rodey Posted June 25, 2013 Share Posted June 25, 2013 (edited) I am writting a document management application which is running a very large if statement, similar to if(($filetype == jpeg)||(filetype == gif)){ $type = "Image"; }elseif... and so on it's about 180 lines. The Problem is I can do it in an array as well, an array will be less code. but is it faster? I am talking large amounts my software knows 600 different extensions with 380 different icons assigned to the file type. I need to know for performance which is faster? and array or an if and ifelse? If I could get assistance that would be good. I would test it myself but rewritting as array for no reason if an if statement is the faster of the two. Thanks. Edited June 25, 2013 by l3rodey Quote Link to comment https://forums.phpfreaks.com/topic/279535-which-is-faster-array-or-if/ Share on other sites More sharing options...
Psycho Posted June 25, 2013 Share Posted June 25, 2013 (edited) Why don't you try testing it? Create a process to run both ways and run it in a loop 100 or 1,000 times and do a timing before and after the loop completes. heck, it might even be faster to just use the database for this. SELECT filtetype FROM types WHERE ext = '$extOfFile' In any case a long string of ifelse() statements is definitely the wrong way to go. With that pattern you should use a switch. But, again, that may or may not be more efficient than an array or using the DB. I would test it myself but rewriting as array for no reason if an if statement is the faster of the two. If you wrote the code for all of that manually you need to approach your coding differently. That is a complete waste of time and energy. I could probably convert your current code to an array or a SQL statement to insert into the DB within 5-10 minutes. Edited June 25, 2013 by Psycho Quote Link to comment https://forums.phpfreaks.com/topic/279535-which-is-faster-array-or-if/#findComment-1437738 Share on other sites More sharing options...
l3rodey Posted June 25, 2013 Author Share Posted June 25, 2013 (edited) Thanks mate, I have them stored in a table for example filename, file extension So your saying I could do the following: SELECT extension FROM files WHERE extension = 'jpeg' || 'gif' Sorry I am not php pro, I am newish this is just a project for learning. What I have in more detail is depending on the file type depends on the file type icon. You can see here secure.cloudbankr.com username: guest password: guest and upload you can see the file icon changes depending on the file type. it is one massive if statement. I learnt if statements before arrays which is why it's an if. I only just learnt arrays and I thought well it looks smaller maybe it's faster and better? so it chooses the type from the database and then creates a variable change depending on the file type so in my div class="<?php echo "$type"; ?> and my if is { type = 'jpeg'; } based on the if... I don't see how a mysql query will filter it to make a different variable each time. without running a query each time. which is not faster. Edited June 25, 2013 by l3rodey Quote Link to comment https://forums.phpfreaks.com/topic/279535-which-is-faster-array-or-if/#findComment-1437740 Share on other sites More sharing options...
Solution Psycho Posted June 25, 2013 Solution Share Posted June 25, 2013 so it chooses the type from the database and then creates a variable change depending on the file type so in my div class="<?php echo "$type"; ?> and my if is { type = 'jpeg'; } based on the if... I don't see how a mysql query will filter it to make a different variable each time. without running a query each time. which is not faster. You would create a table in your database that lists ever file type and any information about that file type (e.g. the display icon) tha you want to store. Then you simply query that information as you need it. If this is a document management system I have to assume you are storing the meta data associated with these files in the database. If that is the case you have to already be querying the information about the files to display on the page. If so, you only need to have a filetypes table and do a JOIN query when retrieving the information about the files. Then you will already have the icon to display without creating any logic and a hundred or so if() statements. Example "files" table structure file_id, file_name, file_path, file_type_id, etc. Example "filetypes" table file_type_id, file_type_ext, file_type_icon, etc. Then, when you are going to INSERT a new file you determine the ext and do a SELECT query to get the file_type_id. Then do an INSERT into the files table with the requisite file info and the file_type_id. (note: I would include a default file_type_id to use when there is no matching extension) Then whenever you need to display the files on a page, you can include the file icon to use as part of the query to get the info SELECT * FROM files JOIN filetypes ON files.file_type_id = filetypes.file_type_id Quote Link to comment https://forums.phpfreaks.com/topic/279535-which-is-faster-array-or-if/#findComment-1437744 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.