Top Related Projects
Count your code, quickly.
cloc counts blank lines, comment lines, and physical lines of source code in many programming languages.
Sloc, Cloc and Code: scc is a very fast accurate code counter with complexity calculations and COCOMO estimates written in pure Go
ripgrep recursively searches directories for a regex pattern while respecting your gitignore
Quick Overview
cgag/loc is a fast, command-line tool for counting lines of code in various programming languages. It's written in Rust and aims to be a faster alternative to other line counting tools like cloc or tokei.
Pros
- Fast performance due to Rust implementation
- Supports a wide range of programming languages
- Simple and straightforward command-line interface
- Provides both summary and detailed output options
Cons
- Limited customization options compared to some other tools
- May not be as actively maintained as more popular alternatives
- Documentation could be more comprehensive
- Lacks some advanced features found in other line counting tools
Getting Started
To use cgag/loc, follow these steps:
-
Install Rust if you haven't already: https://www.rust-lang.org/tools/install
-
Clone the repository:
git clone https://github.com/cgag/loc.git
-
Build the project:
cd loc cargo build --release
-
Run the tool on a directory:
./target/release/loc /path/to/your/code
For example, to count lines in the current directory:
./target/release/loc .
To get more detailed output:
./target/release/loc -v .
Competitor Comparisons
Count your code, quickly.
Pros of tokei
- Written in Rust, offering better performance and memory safety
- Supports a wider range of programming languages (150+)
- Provides more detailed output, including comments and blanks
Cons of tokei
- Larger binary size due to Rust compilation
- Slightly more complex installation process
Code comparison
tokei:
let mut languages = Languages::new();
languages.get_statistics(&[PathBuf::from(".")], &[], &Config::default());
loc:
files, err := loc.FindFiles(".", loc.DefaultExts)
if err != nil {
log.Fatal(err)
}
Key differences
- tokei is more feature-rich and supports more languages
- loc is simpler and easier to install, with a smaller binary size
- tokei provides more detailed output, while loc focuses on simplicity
- Both tools are efficient, but tokei may have a slight performance edge due to Rust
Use cases
- tokei: Ideal for large projects with diverse language requirements and need for detailed statistics
- loc: Better suited for quick, simple line counting tasks or projects with limited language variety
cloc counts blank lines, comment lines, and physical lines of source code in many programming languages.
Pros of cloc
- More comprehensive language support (over 500 languages)
- Advanced features like duplicate file detection and XML output
- Actively maintained with regular updates
Cons of cloc
- Slower performance, especially for large codebases
- More complex to use, with numerous command-line options
Code Comparison
cloc (Perl):
sub count_lines {
my ($file) = @_;
open(my $fh, '<', $file) or die "Can't open $file: $!";
my $count = 0;
while (<$fh>) { $count++; }
close $fh;
return $count;
}
loc (Rust):
fn count_lines(path: &Path) -> io::Result<usize> {
let file = File::open(path)?;
let reader = BufReader::new(file);
Ok(reader.lines().count())
}
Key Differences
- cloc is written in Perl, while loc is written in Rust
- loc focuses on simplicity and speed, while cloc offers more features
- cloc provides detailed statistics, whereas loc aims for a quick overview
- loc is designed for modern development workflows, while cloc has been around longer and has broader compatibility
Both tools serve the purpose of counting lines of code, but they cater to different use cases and preferences. cloc is better suited for detailed analysis and reporting, while loc excels in speed and simplicity for quick checks.
Sloc, Cloc and Code: scc is a very fast accurate code counter with complexity calculations and COCOMO estimates written in pure Go
Pros of scc
- More comprehensive language support (over 200 languages)
- Advanced features like complexity calculations and duplicate detection
- Actively maintained with regular updates
Cons of scc
- Slower performance for large codebases compared to loc
- More complex to use with additional flags and options
- Larger binary size due to more features
Code comparison
loc:
pub fn count_lines(path: &Path) -> io::Result<u64> {
let file = File::open(path)?;
let reader = BufReader::new(file);
Ok(reader.lines().count() as u64)
}
scc:
func CountLines(filename string) (int, error) {
r, err := os.Open(filename)
if err != nil {
return 0, err
}
defer r.Close()
return lineCounter(r), nil
}
Both projects aim to count lines of code, but scc offers more advanced features at the cost of increased complexity. loc is simpler and faster for basic line counting, while scc provides a more comprehensive analysis of codebases. The code snippets show that loc uses Rust and focuses on a straightforward line counting approach, whereas scc is written in Go and likely incorporates more sophisticated counting methods to support its additional features.
ripgrep recursively searches directories for a regex pattern while respecting your gitignore
Pros of ripgrep
- Faster performance, especially for large codebases
- More advanced search capabilities, including regex support
- Cross-platform compatibility (Windows, macOS, Linux)
Cons of ripgrep
- More complex to use, with a steeper learning curve
- Larger binary size and resource footprint
- May be overkill for simple line counting tasks
Code comparison
ripgrep:
use grep_regex::RegexMatcher;
use grep_searcher::Searcher;
use ignore::WalkBuilder;
let matcher = RegexMatcher::new(r"fn\s+\w+").unwrap();
let mut searcher = Searcher::new();
loc:
func countLines(path string) (int, error) {
file, err := os.Open(path)
if err != nil {
return 0, err
}
defer file.Close()
Summary
ripgrep is a powerful, fast, and feature-rich search tool that excels in large codebases and complex search scenarios. It offers advanced regex support and cross-platform compatibility. However, it may be more complex to use and have a larger footprint compared to loc.
loc, on the other hand, is a simpler, more focused tool specifically designed for counting lines of code. It's easier to use for basic line counting tasks but lacks the advanced search capabilities and performance optimizations of ripgrep.
Choose ripgrep for versatile code searching and analysis, or loc for quick and straightforward line counting in smaller projects.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
2019-10-07: I really haven't been on top of accepting pull requests or looking at issues, you guy should definitely look at SCC. It's faster and more accurate than this, and Boyter has written a great series of blog posts detailing how it got this way: https://boyter.org/posts/sloc-cloc-code/
2018-03-08:
I saw a bunch of stars pop up and thought I should mention that tokei is smarter and more accurate so please give that a look and see if there are any wild discrepancies (mostly for your benefit but please let me know if so). Tokei is linked below but it's also rust so cargo install tokei
is all you need. Also these benchmarks are quite old. I doubt cloc has changed but tokei probably has.
loc
is a tool for counting lines of code. It's a rust implementation of cloc, but it's more than 100x faster. There's another rust code counting tool called tokei, loc is ~2-10x faster than tokei, depending on how many files are being counted.
I can count my 400k file src
directory (thanks npm) in just under 7 seconds with loc, in a 1m14s with tokei, and I'm not even willing to try with cloc.
Counting just the dragonflybsd codebase (~9 million lines):
- loc: 1.09 seconds
- tokei: 5.3 seconds
- cloc: 1 minute, 50 seconds
Installation
There are binaries available on the releases page, thanks to the wonderful rust-everywhere project and travisci. For anyone familiar with Rust there's cargo install loc
.
If you want to install Rust/Cargo, this is probably the easiest way: https://www.rustup.rs/.
Windows
loc
should now compile on Windows, but you can also run it under Windows using linux emulation:
You can run
loc
on Windows 10 Anniversary Update build 14393 or later using the Windows Subsystem for Linux. Simply download the Linux distribution from the releases page, and run it inbash
using a WSL-compatible path (e.g./mnt/c/Users/Foo/Repo/
instead ofC:\Users\Foo\Repo
).
Usage
By default, loc
will count lines of code in a target directory:
$ loc
--------------------------------------------------------------------------------
Language Files Lines Blank Comment Code
--------------------------------------------------------------------------------
Lua 2 387088 24193 193544 169351
Rust 4 1172 111 31 1030
C 4 700 75 155 470
Markdown 2 249 39 0 210
Bourne Shell 4 228 41 27 160
Ada 2 53 12 9 32
Toml 1 26 4 2 20
Gherkin 1 12 2 2 8
OCaml 1 13 4 6 3
Ruby 1 4 0 2 2
Handlebars 1 4 0 2 2
--------------------------------------------------------------------------------
Total 23 389549 24481 193780 171288
--------------------------------------------------------------------------------
You can also pass one or many targets for it to inspect
$ loc ci benches
--------------------------------------------------------------------------------
Language Files Lines Blank Comment Code
--------------------------------------------------------------------------------
Bourne Shell 4 228 41 27 160
Rust 1 17 4 0 13
--------------------------------------------------------------------------------
Total 5 245 45 27 173
--------------------------------------------------------------------------------
To see stats for each file parsed, pass the --files
flag:
$ loc --files src
--------------------------------------------------------------------------------
Language Files Lines Blank Comment Code
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
Rust 2 1028 88 29 911
--------------------------------------------------------------------------------
|src/lib.rs 677 54 19 604
|src/main.rs 351 34 10 307
By default, the columns will be sorted by Code
counted in descending order. You can select a different column to sort
using the --sort
flag:
$ loc --files --sort Comment ci
--------------------------------------------------------------------------------
Language Files Lines Blank Comment Code
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
Bourne Shell 4 228 41 27 160
--------------------------------------------------------------------------------
|ci/before_deploy.sh 68 15 13 40
|ci/install.sh 60 13 6 41
|ci/script.sh 41 8 8 25
|ci/utils.sh 59 5 0 54
loc
can also be called with regexes to match and/or exclude files.
$ loc --include 'count'
--------------------------------------------------------------------------------
Language Files Lines Blank Comment Code
--------------------------------------------------------------------------------
Rust 2 144 23 2 119
--------------------------------------------------------------------------------
Total 2 144 23 2 119
loc --exclude 'sh$'
--------------------------------------------------------------------------------
Language Files Lines Blank Comment Code
--------------------------------------------------------------------------------
Lua 2 387088 24193 193544 169351
Rust 4 1172 111 31 1030
C 4 700 75 155 470
Markdown 2 275 38 0 237
Ada 2 53 12 9 32
Toml 1 26 4 2 20
Gherkin 1 12 2 2 8
OCaml 1 13 4 6 3
Handlebars 1 4 0 2 2
Ruby 1 4 0 2 2
--------------------------------------------------------------------------------
Total 19 389347 24439 193753 171155
--------------------------------------------------------------------------------
Known Issues
Fortran has a rule that comments must start with the first character of a line. I only check if it's the first non-whitespace character of a line. I don't know how often this is a problem in real code. I would think not often.
Comments inside string literals: You can get incorrect counts if your code has something like this:
x = "/* I haven't slept \
for 10 days \
because that would be too long \
*/";
loc counts the first line and last lines correctly as code, but the middle lines will be incorrectly counted as comments.
Ignored and hidden files:
By default, loc respects .gitignore/.ignore files, and ignores hidden files and directories. You can count disregard
ignore files with loc -u
, and include hidden files/dirs with loc -uu
.
Supported Languages
- ActionScript
- Ada
- Agda
- AmbientTalk
- ASP
- ASP.NET
- Assembly
- Autoconf
- Awk
- Batch
- Bourne Shell
- C
- C Shell
- C/C++ Header
- C#
- C++
- Clojure
- CoffeeScript
- ColdFusion
- ColdFusionScript
- Coq
- CSS
- CUDA
- CUDA Header
- D
- Dart
- DeviceTree
- Erlang
- Forth
- FORTRAN Legacy
- FORTRAN Modern
- F# (Fsharp)
- GLSL
- Go
- Groovy
- Handlebars
- Haskell
- Hex
- HTML
- Idris
- INI
- Intel Hex
- Isabelle
- Jai
- Java
- JavaScript
- JSON
- Jsx
- Julia
- Kotlin
- Lean
- Less
- LinkerScript
- Lisp
- Lua
- Make
- Makefile
- Markdown
- Mustache
- Nim
- Nix
- Objective-C
- Objective-C++
- OCaml
- OpenCL
- Oz
- Pascal
- Perl
- PHP
- Plain Text
- Polly
- PowerShell
- Prolog
- Protobuf
- Pyret
- Python
- Qcl
- QML
- R
- Razor
- reStructuredText
- Ruby
- RubyHtml
- Rust
- SaltStack
- Sass
- Scala
- SML
- Solidity
- SQL
- Stylus
- Swift
- Tcl
- Terraform
- TeX
- Toml
- TypeScript
- Tsx
- UnrealScript
- VimL
- Wolfram
- XML
- Yacc
- YAML
- Zig
- Z Shell
Attributions
This project contains code from Tokei by Aaronepower and ripgrep by BurntSushi.
Contributors
Top Related Projects
Count your code, quickly.
cloc counts blank lines, comment lines, and physical lines of source code in many programming languages.
Sloc, Cloc and Code: scc is a very fast accurate code counter with complexity calculations and COCOMO estimates written in pure Go
ripgrep recursively searches directories for a regex pattern while respecting your gitignore
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot