Jump to content
MakeWebGames

How many lines of code do you have?


Floydian

Recommended Posts

<?php # Count total line numbers

/*
Apply a recursive directory/file reading function
that will total up how many lines all the .php and .js files
in the directory, and sub-directories starting from where this file is placed.
*/

function line_counter($dir) {
//	echo "Dir: $dir  ----------- <hr> ";
$files = scandir($dir);

static $count = 0;

foreach ($files as $key =>$filename) {
	if (in_array($key, array(0,1)) or in_array($filename, array('purifier'))) {
		continue;
	}
	if (is_dir($dir.$filename)) {
		$new_path = $dir . $filename . '/';
		line_counter($new_path);
	} else {
		$link_name = explode('.',$filename);
		if (!isset($link_name[1]) or !in_array($link_name[1], array('php', 'js')) ) {

		} else {
			$count += count(file($dir.$filename, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES));
		}
//			echo "$dir.$filename<hr>";
	}


}

return $count;
} // End of line_counter() function.

$count = line_counter('./');
echo "<h1>final tally: $count</h1>";
?>

 

This code will recursively search through directories starting from where this script is placed, and count up how many lines of code are contained in your files.

Uncomment this line, if you want to see all the directories searched.

//	echo "Dir: $dir  ----------- <hr> ";

 

Uncomment this line of you want to see all the files that were included in the count.

//			echo "$dir.$filename<hr>";

 

This next line serves two purposes:

1. skip the . and .. which on my system are always the elements in the array with keys of 0 and 1. This is system dependant. You'll have to view the array using var dump or print_r on the $files array to see where it is on your system.

2. The second part filters out certain directories so they aren't included. I did not want third party libraries included in the count.

 

if (in_array($key, array(0,1)) or in_array($filename, array('purifier'))) {

 

This last part filters out file names that don't have an extension, or that don't have an extension we want to count. I chose to only count php and js files.

 

if (!isset($link_name[1]) or !in_array($link_name[1], array('php', 'js')) ) {

 

My count:

final tally: 39672

Edit:

set_time_limit(0);

was removed from the code, it's unnecessary.

Link to comment
Share on other sites

Re: How many lines of code do you have?

Final Tally: 1,317,167

I must have a lot of junk....

Not sure why you need to set the time limit; mine only took a few seconds.

EDIT: I tried to run on my entire directory but it ran out of memory...

Link to comment
Share on other sites

Guest Anonymous

Re: How many lines of code do you have?

Hmm, current project: 917,555

(DBS/MCcode kinda, but 100% fresh code)

Oops - forgot the CSS and JavaScript

1,452,300

Although I will own up and say I didn't write all the JavaScript ;)

EDIT Hmm, seems I may have been a trifle optimistic here... The system I use for generating pages itself writes files which I added in by mistake. Guess I'll have to drop my initial figure by rather a lot

Link to comment
Share on other sites

Re: How many lines of code do you have?

It might be php >= 5 dependant. There's nothing this code depends on as far as php includes. It is being run on php 5.2.5, apache 1.3, and windows xp. There's no mysql involved.

One thing about file systems, different operating systems will have to be compensated for. But if you have my set (perhaps apache version doesn't matter) it should run out of the box, if your php is up to date.

Mccodes? I dun see how it's mccodes (kinda) lol

But yeah, Nyna you are right, it's 100% my code ;)

 

Edit:

Oh, and Will you're absolutely right about the time setting, I should have taken it out of there. I'm sure you can identify with making recursive functions and thinking, well, it took too long to execute, so perhaps I need to allow more time. Then ya find out that your code was just wrong. lol

So yeah, it's a remnant. I'll edit it out.

Link to comment
Share on other sites

Re: How many lines of code do you have?

I made something like this a while back, just to show off... d:

But mine takes long in my directories(the big ones), so I used yours, with only one problem to it.

When it goes on for a bit, at about the 6th directory, it starts to recount all the directories, here is an example:

Dir: ./blah/../blah/../blah/../blah/../blah/../blah/../blah/../blah/../blah/../PPW/ ----------- 
Dir: ./blah/../blah/../blah/../blah/../blah/../blah/../blah/../blah/../blah/../PPW/cache/ ----------- 
Dir: ./blah/../blah/../blah/../blah/../blah/../blah/../blah/../blah/../blah/../PPW/net/ ----------- 

 

I don't know why this is doing this, but I had to add the following to your code to fix it(simple but works):

// original statement
if (is_dir($dir.$filename)) {
		$new_path = $dir . $filename . '/';
		line_counter($new_path);
} 

 

// my alteration
if (is_dir($dir.$filename) &&  !strstr($new_path, '/../')) {
		$new_path = $dir . $filename . '/';
		line_counter($new_path);
}

 

final tally: 1,870,155

I have allot of junk though..

Link to comment
Share on other sites

Re: How many lines of code do you have?

Damn, where's everybody getting these numbers from? The most I managed on a game I built was about 30,000. Maybe we have different definitions of 'a lot of junk'.

edit; made it to 500k, by scanning my entire www directory ... which half of that I didn't even make I guess. Seriously, any examples of a file that would even make a dent in 1mil rows? :\

Link to comment
Share on other sites

Re: How many lines of code do you have?

Well, while I don't call into question anyone's claims (and I don't mean to suggest Decepti0n does either) I do have a lingering question in my mind as to how long it would take to write a million lines of code.

So I did some calculations, because a number like 1 million is hard to grapple wholesale.

Assuming an 8 hour day of work, 7 days a week, and writting code at a rate of 1 line every second solid during those 8 hours of every day, then it would take 34.7 days to write 1 million lines of code.

If we assume that each line takes 30 seconds to write, and again assuming a 7 day work week, 8 hour work day, and a solid one line for every 30 seconds on the job, it would take 1041.7 days to write 1 million lines of code or 2.85 years (again, working every day, 8 hours per day, no breaks, and 1 line of code every 30 seconds.)

It gets even crazier if we add in some other things. Let's say 20% of your time is spent thinking and not writing at all. That leaves 6.4 hours left. Then we'll take out a 30 minute lunch break. 5.9 hours. Then we'll assume a 5 day work week. (this adds in 2 days, for every 5 days in the final total) and if the final total goes over a year (which we know it will), we'll add in two weeks vacation time. lol

With this scenario, we'll assume that the coder is coding at a solid rate of 1 line every 30 seconds while he/she is actively involved in coding.

1412.43 Days (before adding in weekends)

1977.40 Days (with weekends) That's 5.4 years

Five vacations, two weeks each is 70 days.

The grand total is 2047.40 days to write one million lines of code, working a solid 5.9 hours per day, writing one line of code every 30 seconds, taking two days off per week, taking a two week vacation every year.

 

Edit: I incorectly went from 6.4 hours to 6.1 hours adding in a lunch break. This of course resulted in faster coding times. The new calculation uses a 5.9 hour work day which is 30 minutes off of 6.4 hours. ;)

Link to comment
Share on other sites

Guest Anonymous

Re: How many lines of code do you have?

That's a very good point, however I'm sure you use code generators from time to time...

I have large blocks of fairly similar code that is often in a simple format: text, csv, xml etc., that is used to generate the main php files which then need little editing to be up and running.

There's also the case of cut 'n' paste. A lot of my front end script are very similar - a large try { } catch { } block, which I freely admit to writing ... once ;)

It's difficult to get a true representation of what we write - after all - look at my own site - if you view source on any page you will see one line. In fact each page is multiple lines (hence me counting the templates), but rendered as one.

Link to comment
Share on other sites

Re: How many lines of code do you have?

Was alterating this...

 

<?php # Count total line numbers
/*
Apply a recursive directory/file reading function
that will total up how many lines all the .php and .js files
in the directory, and sub-directories starting from where this file is placed.
*/
echo '<title>Line Counter</title>';
function line_counter($dir) 
{
  //echo "Dir: $dir  ----------- <hr>"; //take away the //if you want to display all the directories searched.
  $files = scandir($dir);
  static $count = 0;
  foreach ($files as $key => $filename)
   {
     if (in_array($key, array(0,1)) or in_array($filename, array('purifier')))
      {
 continue;
      }
     if (is_dir($dir.$filename)) 
      {
 $new_path = $dir . $filename . '/';
 line_counter($new_path);
      } 
     else 
      {
 $link_name = explode('.',$filename);
 if (!isset($link_name[1]) or !in_array($link_name[1], array('php', 'js')))
         {
           //Do something...
  } 
        else
         {
    $count += count(file($dir.$filename, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES));
  }
        //echo "$dir.$filename<hr>"; //Remove the //if you want it to display all the files that were included in the count.
      }
   }
  return $count;
} // End of line_counter() function.
$count = line_counter("./");
echo '<h1>Final Tally: '.number_format($count).'</h1>';
?>

 

I used that and got this on www.finalkillah.com/zender.php

 

Final Tally: 18,095

Link to comment
Share on other sites

Re: How many lines of code do you have?

 

...I'm sure you use code generators from time to time...

....

There's also the case of cut 'n' paste. ....

...

[sNIPPED by floydian]

Very true. And I don't fault anyone for doing that :D

Of course every instance of copying lines can't be accounted for with this script.

I skipped over the htmlpurifier code as there is quite a bit of code there that I didn't write, and although it could be included in the count, I wanted to know how many lines I wrote.

Code generators, nope, can't say that I've used that. I do use an ajax javascript function over and over again. That function was not orginally writen by me. Every one of them has some amount of editing. I did include all of those since there is quite a bit of javascript that I did write, and there's no easy way to eliminate them.

In the end, my total of 39k lines of code is an overestimate of how many lines I've written. It would be interesting to include the html purifier and any other 3rd party code just to see how many lines the entire project is.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...