0

I've just finished writing a pretty big class, and its turned out to be noticeably slow (when executing a function from).

The class contains 3 public functions and atleast 10 private functions, resulting in 1,569 lines of code (a fair amount of that is also comments).

Now I must admit, there is allot of singing and dancing going on behind the scenes when calling one of the public functions, but the response time is so bad, the class is not even worth using.

The class file size is 57KB. I'm wondering if the file size is the problem here, or is it just simply because of the code I'm executing - So can I just break the file down, or am I going to have to make drawbacks to my code?

I've also tried simply including the class file from another file, and the same goes...

In case this does any good:

1.) There are quite a few functions in the class that involve file reading/writing (such as file_exists(),fopen(),fwrite(),ect ect)

2.) There are no database queries/connections

3.) There are no huge (over count of 20) loops

Any help is much appreciated!

  • 2
    The file size (57KB / 1569 lines) shouldn't be the reason for poor performance. If possible, use a [php profiler](http://stackoverflow.com/questions/21133/simplest-way-to-profile-a-php-script) to help trace the execution and show what code/functions or combination thereof is taking so long to execute. – drew010 May 02 '12 at 22:57

2 Answers2

1

IO is very likely your slowest operation.

Size of program itself isn't much of an issue. You can profile your code with xdebug to determine what specifically is dragging you down. Using it, you can prioritize what code you will optimize first.

Brad
  • 159,648
  • 54
  • 349
  • 530
0

I've had big files with no problems in terms of speed.

Most likely reason for slowness if because you're not using file handles efficiently. For example, if you close a file after every time you use it it will take significantly longer as opposed to closing it at the end of the script (or just letting it be implicitly closed by not closing it manually).

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • I don't think its the handles. Although each function contains different file operations (`fwriting(), freading() ect ect`) I parse the handles through out, so I use one handle, for each file. –  May 02 '12 at 23:07