Baike.dev
All toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
L

loc

> 编程语言
Open source

Count lines of code quickly.

2.5K stars0 likes0 views
WebsiteGitHub

About

Count lines of code quickly.

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 in bash using a WSL-compatible path (e.g. /mnt/c/Users/Foo/Repo/ instead of C:\Users\Foo\Repo).

Usage

By default, loc will count lines of code in a target directory:

…

You can also pass one or many targets for it to inspect

…

To see stats for each file parsed, pass the --files flag:

…

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 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
…

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

GitHub Issues· 0 open

View all on GitHub

No open issues yet, or sync has not completed.

Highlights

  • •loc: 1.09 seconds
  • •tokei: 5.3 seconds
  • •cloc: 1 minute, 50 seconds
  • •ActionScript
  • •AmbientTalk
  • •Assembly
  • •Autoconf
  • •Bourne Shell
  • •C/C++ Header
  • •CoffeeScript

> Tags

Rust

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言