以下の内容はhttps://kazuhira-r.hatenablog.com/entry/2025/10/04/163206より取得しました。


cloc、scc、tokeiでソースコードの規模を計測する

これは、なにをしたくて書いたもの?

個人的にはあまりやらないのですが、ソースコードの規模を計測するツールを動かしておこうかなと思いまして。

いわゆるSLOC計測ツール

ソースコードの規模を計測するツールとしては、このあたりが有名のようです。

cloc。

GitHub - AlDanial/cloc: cloc counts blank lines, comment lines, and physical lines of source code in many programming languages.

多くのプログラミング言語を対象に、空行数、コメント行数、物理的な行数をカウントしてくれるようです。

対応言語は確かに多いですね…。

cloc / Recognized Languages

計測は、ファイル、ディレクトリー、zipファイル、Gitリポジトリーの特定のコミットなどを対象にできるようです。

cloc / Quick Start

scc。

GitHub - boyter/scc: Sloc, Cloc and Code: scc is a very fast accurate code counter with complexity calculations and COCOMO estimates written in pure Go

こちらも多くのプログラミング言語のコードの行数、空行数、コメント行数、物理的な行数をカウントするツールです。cloc、sloccount、tokeiに
似たものだと言っています。

対応している言語はこちらのようです。

https://github.com/boyter/scc/blob/v3.5.0/LANGUAGES.md

tokei(時計)。

GitHub - XAMPPRocky/tokei: Count your code, quickly.

非常に高速だと謳っています。

対応言語はこちら。

Tokei (時計) / Supported Languages

最新安定版は少し古いのですが、v13がずっと開発中のようです。

見ていってもなんなので、実際に試してみましょう。

環境

今回の環境はこちら。

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 24.04.3 LTS
Release:        24.04
Codename:       noble


$ uname -srvmpio
Linux 6.8.0-85-generic #85-Ubuntu SMP PREEMPT_DYNAMIC Thu Sep 18 15:26:59 UTC 2025 x86_64 x86_64 x86_64 GNU/Linux

お題

なにかリポジトリーで計測してみましょう。今回はRESTEasyとFastAPIにしてみます。

cloneして準備しておきます。

# RESTEasy
$ git clone https://github.com/resteasy/resteasy.git
$ cd resteasy
$ git checkout 6.2.11.Final


# FastAPI
$ git clone https://github.com/fastapi/fastapi.git
$ cd fastapi
$ git checkout 0.118.0

比較のために、単純な行数もそれぞれ見ておきましょう。

# RESTEasy
$ wc -l resteasy-core/src/main/java/org/jboss/resteasy/core/SynchronousDispatcher.java
554 resteasy-core/src/main/java/org/jboss/resteasy/core/SynchronousDispatcher.java


# FastAPI
$ wc -l fastapi/applications.py
4667 fastapi/applications.py

cloc

それでは、順番に見ていきましょう。

まずはclocから。

clocはaptで入れると少し古いものになるので、バイナリーをダウンロードしてきて使いましょう。

$ curl -LO https://github.com/AlDanial/cloc/releases/download/v2.06/cloc-2.06.tar.gz
$ tar xf cloc-2.06.tar.gz
$ cp cloc-2.06/cloc ~/.local/bin

バージョン。

$ cloc --version
2.06

ヘルプ。

$ cloc --help

Usage: cloc [options] <file(s)/dir(s)/git hash(es)> | <set 1> <set 2> | <report files>

 Count, or compute differences of, physical lines of source code in the
 given files (may be archives such as compressed tarballs or zip files,
 or git commit hashes or branch names) and/or recursively below the
 given directories.

 Input Options
   --extract-with=<cmd>      This option is only needed if cloc is unable
                             to figure out how to extract the contents of
                             the input file(s) by itself.
                             Use <cmd> to extract binary archive files (e.g.:
                             .tar.gz, .zip, .Z).  Use the literal '>FILE<' as
                             a stand-in for the actual file(s) to be
                             extracted.  For example, to count lines of code
                             in the input files
                                gcc-4.2.tar.gz  perl-5.8.8.tar.gz
                             on Unix use
                               --extract-with='gzip -dc >FILE< | tar xf -'
                             or, if you have GNU tar,
                               --extract-with='tar zxf >FILE<'
                             and on Windows use, for example:
                               --extract-with="\"c:\Program Files\WinZip\WinZip32.exe\" -e -o >FILE< ."
                             (if WinZip is installed there).
   --list-file=<file>        Take the list of file and/or directory names to
                             process from <file>, which has one file/directory
                             name per line.  Only exact matches are counted;
                             relative path names will be resolved starting from
                             the directory where cloc is invoked.  Set <file>
                             to - to read file names from a STDIN pipe.
                             See also --exclude-list-file, --config.
   --diff-list-file=<file>   Take the pairs of file names to be diff'ed from
                             <file>, whose format matches the output of
                             --diff-alignment.  (Run with that option to
                             see a sample.)  The language identifier at the
                             end of each line is ignored.  This enables --diff
                             mode and bypasses file pair alignment logic.
                             Use --diff-list-files to define the file name
                             pairs in separate files. See also --config.
   --diff-list-files <file1> <file2>
                             Compute differences in code and comments between
                             the files and directories listed in <file1> and
                             <file2>.  Each input file should use the same
                             format as --list-file, where there is one file or
                             directory name per line.  Only exact matches are
                             counted; relative path names will be resolved
                             starting from the directory where cloc is invoked.
                             This enables --diff mode.  See also --list-file,
                             --diff-list-file, --diff.
   --files-from=<CMD>        Synonym for --vcs=<CMD>.
   --vcs=<CMD>               Invoke a system call to <CMD> to obtain a list of
                             files to work on.  If <CMD> is 'git', then will
                             invoke 'git ls-files' to get a file list and
                             'git submodule status' to get a list of submodules
                             whose contents will be ignored.  See also --git
                             which accepts git commit hashes and branch names.
                             If <VCS> is 'svn' then will invoke 'svn list -R'.
                             The primary benefit is that cloc will then skip
                             files explicitly excluded by the versioning tool
                             in question, ie, those in .gitignore or have the
                             svn:ignore property.
                             Alternatively <CMD> may be any system command
                             that generates a list of files.
                             Note:  cloc must be in a directory which can read
                             the files as they are returned by <CMD>.  cloc will
                             not download files from remote repositories.
                             'svn list -R' may refer to a remote repository
                             to obtain file names (and therefore may require
                             authentication to the remote repository), but
                             the files themselves must be local.
                             Setting <CMD> to 'auto' selects between 'git'
                             and 'svn' (or neither) depending on the presence
                             of a .git or .svn subdirectory below the directory
                             where cloc is invoked.
                             --files-from is a synonym for --vcs.
   --unicode                 Check binary files to see if they contain Unicode
                             expanded ASCII text.  This causes performance to
                             drop noticeably.

 Processing Options
   --autoconf                Count .in files (as processed by GNU autoconf) of
                             recognized languages.  See also --no-autogen.
   --by-file                 Report results for every source file encountered.
                             See also --fmt under 'Output Options'.
   --by-file-by-lang         Report results for every source file encountered
                             in addition to reporting by language.
   --config <file>           Read command line switches from <file> instead of
                             the default location of /home/vagrant/.config/cloc/options.txt.
                             The file should contain one switch, along with
                             arguments (if any), per line.  Blank lines and lines
                             beginning with '#' are skipped.  Options given on
                             the command line take priority over entries read from
                             the file.
                             If a directory is also given with any of these
                             switches: --list-file, --exclude-list-file,
                             --read-lang-def, --force-lang-def, --diff-list-file
                             and a config file exists in that directory, it will
                             take priority over /home/vagrant/.config/cloc/options.txt.
   --count-and-diff <set1> <set2>
                             First perform direct code counts of source file(s)
                             of <set1> and <set2> separately, then perform a diff
                             of these.  Inputs may be pairs of files, directories,
                             or archives.  If --out or --report-file is given,
                             three output files will be created, one for each
                             of the two counts and one for the diff.  See also
                             --diff, --diff-alignment, --diff-timeout,
                             --ignore-case, --ignore-whitespace.
   --diff <set1> <set2>      Compute differences in code and comments between
                             source file(s) of <set1> and <set2>.  The inputs
                             may be any mix of files, directories, archives,
                             or git commit hashes.  Use --diff-alignment to
                             generate a list showing which file pairs where
                             compared.  When comparing git branches, only files
                             which have changed in either commit are compared.
                             See also --git, --count-and-diff, --diff-alignment,
                             --diff-list-file, --diff-timeout, --ignore-case,
                             --ignore-whitespace.
   --diff-timeout <N>        Ignore files which take more than <N> seconds
                             to process.  Default is 10 seconds.  Setting <N>
                             to 0 allows unlimited time.  (Large files with many
                             repeated lines can cause Algorithm::Diff::sdiff()
                             to take hours.) See also --timeout.
   --docstring-as-code       cloc considers docstrings to be comments, but this is
                             not always correct as docstrings represent regular
                             strings when they appear on the right hand side of an
                             assignment or as function arguments.  This switch
                             forces docstrings to be counted as code.
   --follow-links            [Unix only] Follow symbolic links to directories
                             (sym links to files are always followed).
                             See also --stat.
   --force-lang=<lang>[,<ext>]
                             Process all files that have a <ext> extension
                             with the counter for language <lang>.  For
                             example, to count all .f files with the
                             Fortran 90 counter (which expects files to
                             end with .f90) instead of the default Fortran 77
                             counter, use
                               --force-lang="Fortran 90,f"
                             If <ext> is omitted, every file will be counted
                             with the <lang> counter.  This option can be
                             specified multiple times (but that is only
                             useful when <ext> is given each time).
                             See also --script-lang, --lang-no-ext.
   --force-lang-def=<file>   Load language processing filters from <file>,
                             then use these filters instead of the built-in
                             filters.  Note:  languages which map to the same
                             file extension (for example:
                             MATLAB/Mathematica/Objective-C/MUMPS/Mercury;
                             Pascal/PHP; Lisp/OpenCL; Lisp/Julia; Perl/Prolog)
                             will be ignored as these require additional
                             processing that is not expressed in language
                             definition files.  Use --read-lang-def to define
                             new language filters without replacing built-in
                             filters (see also --write-lang-def,
                             --write-lang-def-incl-dup, --config).
   --git                     Forces the inputs to be interpreted as git targets
                             (commit hashes, branch names, et cetera) if these
                             are not first identified as file or directory
                             names.  This option overrides the --vcs=git logic
                             if this is given; in other words, --git gets its
                             list of files to work on directly from git using
                             the hash or branch name rather than from
                             'git ls-files'.  This option can be used with
                             --diff to perform line count diffs between git
                             commits, or between a git commit and a file,
                             directory, or archive.  Use -v/--verbose to see
                             the git system commands cloc issues.
   --git-diff-rel            Same as --git --diff, or just --diff if the inputs
                             are recognized as git targets.  Only files which
                             have changed in either commit are compared.
   --git-diff-all            Git diff strategy #2:  compare all files in the
                             repository between the two commits.
   --ignore-whitespace       Ignore horizontal white space when comparing files
                             with --diff.  See also --ignore-case.
   --ignore-case             Ignore changes in case within file contents;
                             consider upper- and lowercase letters equivalent
                             when comparing files with --diff.  See also
                             --ignore-whitespace.
   --ignore-case-ext         Ignore case of file name extensions.  This will
                             cause problems counting some languages
                             (specifically, .c and .C are associated with C and
                             C++; this switch would count .C files as C rather
                             than C++ on *nix operating systems).  File name
                             case insensitivity is always true on Windows.
   --ignore-regex            Ignore lines in source files that match the given
                             Perl regular expression for the given language(s).
                             This option can be specified multiple times.
                             Language names are comma separated and are followed
                             by the pipe character and the regular expression.
                             Use * to match all languages.
                             Examples:
                               --ignore-regex="C,Java,C++|^\s*[{};]\s*$"
                               --ignore-regex="*|DEBUG|TEST\s+ONLY"
                             These filters are applied after comments are
                             removed.  Use --strip-comments=EXT to create
                             new files that show these filters applied.
                             The primary use case is to ignore lines
                             containing only braces, brackets, or puctuation.
   --lang-no-ext=<lang>      Count files without extensions using the <lang>
                             counter.  This option overrides internal logic
                             for files without extensions (where such files
                             are checked against known scripting languages
                             by examining the first line for #!).  See also
                             --force-lang, --script-lang.
   --max-file-size=<MB>      Skip files larger than <MB> megabytes when
                             traversing directories.  By default, <MB>=100.
                             cloc's memory requirement is roughly twenty times
                             larger than the largest file so running with
                             files larger than 100 MB on a computer with less
                             than 2 GB of memory will cause problems.
                             Note:  this check does not apply to files
                             explicitly passed as command line arguments.
   --no-autogen[=list]       Ignore files generated by code-production systems
                             such as GNU autoconf.  To see a list of these files
                             (then exit), run with --no-autogen list
                             See also --autoconf.
   --no-recurse              Count files in the given directories without
                             recursively descending below them.
   --original-dir            [Only effective in combination with
                             --strip-comments or --strip-code]  Write the stripped
                             files to the same directory as the original files.
   --only-count-files        Only count files by language.  Blank, comment, and
                             code counts will be zero.
   --read-binary-files       Process binary files in addition to text files.
                             This is usually a bad idea and should only be
                             attempted with text files that have embedded
                             binary data.
   --read-lang-def=<file>    Load new language processing filters from <file>
                             and merge them with those already known to cloc.
                             If <file> defines a language cloc already knows
                             about, cloc's definition will take precedence.
                             Use --force-lang-def to over-ride cloc's
                             definitions (see also --write-lang-def,
                             --write-lang-def-incl-dup, --config).
   --script-lang=<lang>,<s>  Process all files that invoke <s> as a #!
                             scripting language with the counter for language
                             <lang>.  For example, files that begin with
                                #!/usr/local/bin/perl5.8.8
                             will be counted with the Perl counter by using
                                --script-lang=Perl,perl5.8.8
                             The language name is case insensitive but the
                             name of the script language executable, <s>,
                             must have the right case.  This option can be
                             specified multiple times.  See also --force-lang,
                             --lang-no-ext.
   --sdir=<dir>              Use <dir> as the scratch directory instead of
                             letting File::Temp chose the location.  Files
                             written to this location are not removed at
                             the end of the run (as they are with File::Temp).
   --skip-leading=<N[,ext]>  Skip the first <N> lines of each file.  If a
                             comma separated list of extensions is also given,
                             only skip lines from those file types.  Example:
                               --skip-leading=10,cpp,h
                             will skip the first ten lines of *.cpp and *.h
                             files.  This is useful for ignoring boilerplate
                             text.
   --skip-uniqueness         Skip the file uniqueness check.  This will give
                             a performance boost at the expense of counting
                             files with identical contents multiple times
                             (if such duplicates exist).
   --stat                    Some file systems (AFS, CD-ROM, FAT, HPFS, SMB)
                             do not have directory 'nlink' counts that match
                             the number of its subdirectories.  Consequently
                             cloc may undercount or completely skip the
                             contents of such file systems.  This switch forces
                             File::Find to stat directories to obtain the
                             correct count.  File search speed will decrease.
                             See also --follow-links.
   --stdin-name=<file>       Give a file name to use to determine the language
                             for standard input.  (Use - as the input name to
                             receive source code via STDIN.)
   --strip-code=<ext>        For each file processed, write to the current
                             directory a version of the file which has blank
                             and code lines, including code with (in-line
                             comments) removed. The name of each stripped file
                             is the original file name with .<ext> appended to
                             it. It is written to the current directory unless
                             --original-dir is on.
   --strip-comments=<ext>    For each file processed, write to the current
                             directory a version of the file which has blank
                             and commented lines removed (in-line comments
                             persist).  The name of each stripped file is the
                             original file name with .<ext> appended to it.
                             It is written to the current directory unless
                             --original-dir is on.
   --strip-str-comments      Replace comment markers embedded in strings with
                             'xx'.  This attempts to work around a limitation
                             in Regexp::Common::Comment where comment markers
                             embedded in strings are seen as actual comment
                             markers and not strings, often resulting in a
                             'Complex regular subexpression recursion limit'
                             warning and incorrect counts.  There are two
                             disadvantages to using this switch:  1/code count
                             performance drops, and 2/code generated with
                             --strip-comments will contain different strings
                             where ever embedded comments are found.
   --sum-reports             Input arguments are report files previously
                             created with the --report-file option in plain
                             format (eg. not JSON, YAML, XML, or SQL).
                             Makes a cumulative set of results containing the
                             sum of data from the individual report files.
   --timeout <N>             Ignore files which take more than <N> seconds
                             to process at any of the language's filter stages.
                             The default maximum number of seconds spent on a
                             filter stage is the number of lines in the file
                             divided by one thousand.  Setting <N> to 0 allows
                             unlimited time.  See also --diff-timeout.
   --processes=NUM           [Available only on systems with a recent version
                             of the Parallel::ForkManager module.  Not
                             available on Windows.] Sets the maximum number of
                             cores that cloc uses.  The default value of 0
                             disables multiprocessing.
   --unix                    Override the operating system autodetection
                             logic and run in UNIX mode.  See also
                             --windows, --show-os.
   --use-sloccount           If SLOCCount is installed, use its compiled
                             executables c_count, java_count, pascal_count,
                             php_count, and xml_count instead of cloc's
                             counters.  SLOCCount's compiled counters are
                             substantially faster than cloc's and may give
                             a performance improvement when counting projects
                             with large files.  However, these cloc-specific
                             features will not be available: --diff,
                             --count-and-diff, --strip-code, --strip-comments,
                             --unicode.
   --windows                 Override the operating system autodetection
                             logic and run in Microsoft Windows mode.
                             See also --unix, --show-os.

 Filter Options
   --include-content=<regex> Only count files containing text that matches the
                             given regular expression.
   --exclude-content=<regex> Exclude files containing text that matches the given
                             regular expression.
   --exclude-dir=<D1>[,D2,]  Exclude the given comma separated directories
                             D1, D2, D3, et cetera, from being scanned.  For
                             example  --exclude-dir=.cache,test  will skip
                             all files and subdirectories that have /.cache/
                             or /test/ as their parent directory.
                             Directories named .bzr, .cvs, .hg, .git, .svn,
                             and .snapshot are always excluded.
                             This option only works with individual directory
                             names so including file path separators is not
                             allowed.  Use --fullpath and --not-match-d=<regex>
                             to supply a regex matching multiple subdirectories.
   --exclude-ext=<ext1>[,<ext2>[...]]
                             Do not count files having the given file name
                             extensions.
   --exclude-lang=<L1>[,L2[...]]
                             Exclude the given comma separated languages
                             L1, L2, L3, et cetera, from being counted.
   --exclude-list-file=<file>  Ignore files and/or directories whose names
                             appear in <file>.  <file> should have one file
                             name per line.  Only exact matches are ignored;
                             relative path names will be resolved starting from
                             the directory where cloc is invoked.
                             See also --list-file, --config.
   --fullpath                Modifies the behavior of --match-f, --not-match-f,
                             and --not-match-d to include the file's path--
                             relative to the directory from which cloc is
                             invoked--in the regex, not just the file's basename.
                             (This does not expand each filename to include its
                             fully qualified absolute path; instead, it uses as
                             much of the path as is passed in to cloc.)
   --include-ext=<ext1>[,ext2[...]]
                             Count only languages having the given comma
                             separated file extensions.  Use --show-ext to
                             see the recognized extensions.
   --include-lang=<L1>[,L2[...]]
                             Count only the given comma separated, case-
                             insensitive languages L1, L2, L3, et cetera.  Use
                             --show-lang to see the list of recognized languages.
   --match-d=<regex>         Only count files in directories matching the Perl
                             regex.  For example
                               --match-d='/(src|include)/'
                             only counts files in directories containing
                             /src/ or /include/.  Unlike --not-match-d,
                             --match-f, and --not-match-f, --match-d always
                             anchors the regex to the directory from which
                             cloc is invoked.
   --not-match-d=<regex>     Count all files except those in directories
                             matching the Perl regex.  Only the trailing
                             directory name is compared, for example, when
                             counting in /usr/local/lib, only 'lib' is
                             compared to the regex.
                             Add --fullpath to compare parent directories, beginning
                             from the directory where cloc is invoked, to the regex.
                             Do not include file path separators at the beginning
                             or end of the regex. This option may be repeated.
   --match-f=<regex>         Only count files whose basenames match the Perl
                             regex.  For example
                               --match-f='^[Ww]idget'
                             only counts files that start with Widget or widget.
                             Add --fullpath to include parent directories
                             in the regex instead of just the basename.
   --not-match-f=<regex>     Count all files except those whose basenames
                             match the Perl regex.  Add --fullpath to include
                             parent directories in the regex instead of just
                             the basename. This option may be repeated.
   --skip-archive=<regex>    Ignore files that end with the given Perl regular
                             expression.  For example, if given
                               --skip-archive='(zip|tar(.(gz|Z|bz2|xz|7z))?)'
                             the code will skip files that end with .zip,
                             .tar, .tar.gz, .tar.Z, .tar.bz2, .tar.xz, and
                             .tar.7z.
   --skip-win-hidden         On Windows, ignore hidden files.

 Debug Options
   --categorized=<file>      Save file sizes in bytes, identified languages
                             and names of categorized files to <file>.
   --counted=<file>          Save names of processed source files to <file>.
                             See also --found, --ignored, --unique.
   --diff-alignment=<file>   Write to <file> a list of files and file pairs
                             showing which files were added, removed, and/or
                             compared during a run with --diff.  This switch
                             forces the --diff mode on.
   --explain=<lang>          Print the filters used to remove comments for
                             language <lang> and exit.  In some cases the
                             filters refer to Perl subroutines rather than
                             regular expressions.  An examination of the
                             source code may be needed for further explanation.
   --help                    Print this usage information and exit.
   --found=<file>            Save names of every file found to <file>.  See
                             also --counted, --ignored, --unique.
   --ignored=<file>          Save names of ignored files and the reason they
                             were ignored to <file>.  See also --counted,
                             --found, --unique.
   --print-filter-stages     Print processed source code before and after
                             each filter is applied.
   --show-ext[=<ext>]        Print information about all known (or just the
                             given) file extensions and exit.
   --show-lang[=<lang>]      Print information about all known (or just the
                             given) languages and exit.
   --show-os                 Print the value of the operating system mode
                             and exit.  See also --unix, --windows.
   --unique=<file>           Save names of unique files found to <file>.  See
                             also --counted, --found, --ignored.
   -v[=<n>]                  Verbose switch (optional numeric value).
   -verbose[=<n>]            Long form of -v.
   --version                 Print the version of this program and exit.
   --write-lang-def=<file>   Writes to <file> the language processing filters
                             then exits.  Useful as a first step to creating
                             custom language definitions. Note: languages which
                             map to the same file extension will be excluded.
                             (See also --force-lang-def, --read-lang-def).
   --write-lang-def-incl-dup=<file>
                             Same as --write-lang-def, but includes duplicated
                             extensions.  This generates a problematic language
                             definition file because cloc will refuse to use
                             it until duplicates are removed.

 Output Options
   --3                       Print third-generation language output.
                             (This option can cause report summation to fail
                             if some reports were produced with this option
                             while others were produced without it.)
   --by-percent  X           Instead of comment and blank line counts, show
                             these values as percentages based on the value
                             of X in the denominator, where X is
                                 c    meaning lines of code
                                 cm   meaning lines of code + comments
                                 cb   meaning lines of code + blanks
                                 cmb  meaning lines of code + comments + blanks
                                 t    meaning sum of values in that column
                             For example, if using method 'c' and your code
                             has twice as many lines of comments as lines
                             of code, the value in the comment column will
                             be 200%.  Method 't' computes percentages
                             based on totals for each column.  Another way of
                             looking at this is that 't' computes percentages
                             vertically while the other methods compute them
                             horizontally.
   --csv                     Write the results as comma separated values.
   --csv-delimiter=<C>       Use the character <C> as the delimiter for comma
                             separated files instead of ,.  This switch forces
   --file-encoding=<E>       Write output files using the <E> encoding instead of
                             the default ASCII (<E> = 'UTF-7').  Examples: 'UTF-16',
                             'euc-kr', 'iso-8859-16'.  Known encodings can be
                             printed with
                               perl -MEncode -e 'print join("\n", Encode->encodings(":all")), "\n"'
   --fmt=<N>                 Alternate text output format where <N> is a number
                             from 1 to 5. 'total lines' means the sum of code,
                             comment, and blank lines.  The formats are:
                               1:  by language (same as cloc default output)
                               2:  by language with an extra column for total lines
                               3:  by file with language
                               4:  by file with a total lines column
                               5:  by file with language and a total lines column
   --hide-rate               Do not show elapsed time, line processing rate, or
                             file processing rates in the output header. This
                             makes output deterministic.
   --json                    Write the results as JavaScript Object Notation
                             (JSON) formatted output.
   --md                      Write the results as Markdown-formatted text.
   --out=<file>              Synonym for --report-file=<file>.
   --percent                 Show counts as percentages of sums for each column.
                             Same as '--by-percent t'.
   --progress-rate=<n>       Show progress update after every <n> files are
                             processed (default <n>=100).  Set <n> to 0 to
                             suppress progress output (useful when redirecting
                             output to STDOUT).
   --quiet                   Suppress all information messages except for
                             the final report.
   --report-file=<file>      Write the results to <file> instead of STDOUT.
   --summary-cutoff=X:N      Aggregate to 'Other' results having X lines
                             below N where X is one of
                                c   meaning lines of code
                                f   meaning files
                                m   meaning lines of comments
                                cm  meaning lines of code + comments
                             Appending a percent sign to N changes
                             the calculation from straight count to
                             percentage.
                             Ignored with --diff or --by-file.
   --sql=<file>              Write results as SQL create and insert statements
                             which can be read by a database program such as
                             SQLite.  If <file> is -, output is sent to STDOUT.
   --sql-append              Append SQL insert statements to the file specified
                             by --sql and do not generate table creation
                             statements.  Only valid with the --sql option.
   --sql-project=<name>      Use <name> as the project identifier for the
                             current run.  Only valid with the --sql option.
   --sql-style=<style>       Write SQL statements in the given style instead
                             of the default SQLite format.  Styles include
                             'Oracle' and 'Named_Columns'.
   --sum-one                 For plain text reports, show the SUM: output line
                             even if only one input file is processed.
   --thousands-delimiter=<C> Divides numbers with many digits (i.e. numbers
                             over 999) into groups using the character <C> as
                             delimiter (e.g. for <C> = '.': 12345 -> 12.345).
                             Only works with the '--fmt' option.
                             Sample values: '.', ',', '_', ' '
                             Synonym:  --ksep
   --xml                     Write the results in XML.
   --xsl=<file>              Reference <file> as an XSL stylesheet within
                             the XML output.  If <file> is 1 (numeric one),
                             writes a default stylesheet, cloc.xsl (or
                             cloc-diff.xsl if --diff is also given).
                             This switch forces --xml on.
   --yaml                    Write the results in YAML.

オプションが多いですね…。

RESTEasyで計測。

$ cloc *
    4416 text files.
    4299 unique files.
     155 files ignored.

github.com/AlDanial/cloc v 2.06  T=3.39 s (1267.6 files/s, 105057.8 lines/s)
-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
Java                          3942          44140          40922         236368
AsciiDoc                        61           3054             59          11301
Maven                           63            506            483           9336
XML                            160            684            774           3795
Properties                      41             27           2534            969
HTML                             2             34              0            182
JavaScript                       1             54            321            166
Text                            10             18              0            132
Python                           7             58             53            101
Markdown                         3             33              0             51
XSD                              2              3              0             51
JSON                             1              0              0             37
Bourne Shell                     1              8              0             16
Org Mode                         1              0              0             16
SQL                              2              0              0              3
DTD                              2              0              0              2
-------------------------------------------------------------------------------
SUM:                          4299          48619          45146         262526
-------------------------------------------------------------------------------

1ファイルを測ってみましょう。

$ cloc resteasy-core/src/main/java/org/jboss/resteasy/core/SynchronousDispatcher.java
       1 text file.
       1 unique file.
       0 files ignored.

github.com/AlDanial/cloc v 2.06  T=0.02 s (45.8 files/s, 25374.5 lines/s)
-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
Java                             1             50             75            429
-------------------------------------------------------------------------------

FastAPI。

$ cloc *
    2222 text files.
    2176 unique files.
     221 files ignored.

github.com/AlDanial/cloc v 2.06  T=1.00 s (2172.4 files/s, 287267.6 lines/s)
-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
Markdown                      1060          74072             64         104639
Python                        1028          12760           7829          68717
SVG                             51              1              0          12717
YAML                            16              0              5           5694
JavaScript                       3             38             71            369
CSS                              3             46             29            229
TOML                             1             24             42            203
HTML                             3              1              0            104
Text                             7              0              0             56
Bourne Shell                     4              7              0             22
-------------------------------------------------------------------------------
SUM:                          2176          86949           8040         192750
-------------------------------------------------------------------------------

1ファイルを対象に計測。

$ cloc fastapi/applications.py
       1 text file.
       1 unique file.
       0 files ignored.

github.com/AlDanial/cloc v 2.06  T=0.03 s (33.8 files/s, 157685.9 lines/s)
-------------------------------------------------------------------------------
Language                     files          blank        comment           code
-------------------------------------------------------------------------------
Python                           1            648           2187           1832
-------------------------------------------------------------------------------

scc

scc。

sccはsnapでも入れられますが、やっぱり少し古いのでバイナリーをダウンロードしてきてインストールします。

$ curl -LO https://github.com/boyter/scc/releases/download/v3.5.0/scc_Linux_x86_64.tar.gz
$ tar xf scc_Linux_x86_64.tar.gz
$ cp scc ~/.local/bin

バージョン。

$ scc --version
scc version 3.5.0

ヘルプ。

$ scc --help
Sloc, Cloc and Code. Count lines of code in a directory with complexity estimation.
Version 3.5.0
Ben Boyter <ben@boyter.org> + Contributors

Usage:
  scc [flags] [files or directories]

Flags:
      --avg-wage int                       average wage value used for basic COCOMO calculation (default 56286)
      --binary                             disable binary file detection
      --by-file                            display output for every file
  -m, --character                          calculate max and mean characters per line
      --ci                                 enable CI output settings where stdout is ASCII
      --cocomo-project-type string         change COCOMO model type [organic, semi-detached, embedded, "custom,1,1,1,1"] (default "organic")
      --count-as string                    count extension as language [e.g. jsp:htm,chead:"C Header" maps extension jsp to html and chead to C Header]
      --count-ignore                       set to allow .gitignore and .ignore files to be counted
      --currency-symbol string             set currency symbol (default "$")
      --debug                              enable debug output
      --directory-walker-job-workers int   controls the maximum number of workers which will walk the directory tree (default 8)
  -a, --dryness                            calculate the DRYness of the project (implies --uloc)
      --eaf float                          the effort adjustment factor derived from the cost drivers (1.0 if rated nominal) (default 1)
      --exclude-dir strings                directories to exclude (default [.git,.hg,.svn])
  -x, --exclude-ext strings                ignore file extensions (overrides include-ext) [comma separated list: e.g. go,java,js]
  -n, --exclude-file strings               ignore files with matching names (default [package-lock.json,Cargo.lock,yarn.lock,pubspec.lock,Podfile.lock,pnpm-lock.yaml])
      --file-gc-count int                  number of files to parse before turning the GC on (default 10000)
      --file-list-queue-size int           the size of the queue of files found and ready to be read into memory (default 4)
      --file-process-job-workers int       number of goroutine workers that process files collecting stats (default 4)
      --file-summary-job-queue-size int    the size of the queue used to hold processed file statistics before formatting (default 4)
  -f, --format string                      set output format [tabular, wide, json, json2, csv, csv-stream, cloc-yaml, html, html-table, sql, sql-insert, openmetrics] (default "tabular")
      --format-multi string                have multiple format output overriding --format [e.g. tabular:stdout,csv:file.csv,json:file.json]
      --gen                                identify generated files
      --generated-markers strings          string markers in head of generated files (default [do not edit,<auto-generated />])
  -h, --help                               help for scc
  -i, --include-ext strings                limit to file extensions [comma separated list: e.g. go,java,js]
      --include-symlinks                   if set will count symlink files
  -l, --languages                          print supported languages and extensions
      --large-byte-count int               number of bytes a file can contain before being removed from output (default 1000000)
      --large-line-count int               number of lines a file can contain before being removed from output (default 40000)
      --min                                identify minified files
  -z, --min-gen                            identify minified or generated files
      --min-gen-line-length int            number of bytes per average line for file to be considered minified or generated (default 255)
      --no-cocomo                          remove COCOMO calculation output
  -c, --no-complexity                      skip calculation of code complexity
  -d, --no-duplicates                      remove duplicate files from stats and output
      --no-gen                             ignore generated files in output (implies --gen)
      --no-gitignore                       disables .gitignore file logic
      --no-gitmodule                       disables .gitmodules file logic
      --no-hborder                         remove horizontal borders between sections
      --no-ignore                          disables .ignore file logic
      --no-large                           ignore files over certain byte and line size set by large-line-count and large-byte-count
      --no-min                             ignore minified files in output (implies --min)
      --no-min-gen                         ignore minified or generated files in output (implies --min-gen)
      --no-scc-ignore                      disables .sccignore file logic
      --no-size                            remove size calculation output
  -M, --not-match stringArray              ignore files and directories matching regular expression
  -o, --output string                      output filename (default stdout)
      --overhead float                     set the overhead multiplier for corporate overhead (facilities, equipment, accounting, etc.) (default 2.4)
  -p, --percent                            include percentage values in output
      --remap-all string                   inspect every file and remap by checking for a string and remapping the language [e.g. "-*- C++ -*-":"C Header"]
      --remap-unknown string               inspect files of unknown type and remap by checking for a string and remapping the language [e.g. "-*- C++ -*-":"C Header"]
      --size-unit string                   set size unit [si, binary, mixed, xkcd-kb, xkcd-kelly, xkcd-imaginary, xkcd-intel, xkcd-drive, xkcd-bakers] (default "si")
      --sloccount-format                   print a more SLOCCount like COCOMO calculation
  -s, --sort string                        column to sort by [files, name, lines, blanks, code, comments, complexity] (default "files")
      --sql-project string                 use supplied name as the project identifier for the current run. Only valid with the --format sql or sql-insert option
  -t, --trace                              enable trace output (not recommended when processing multiple files)
  -u, --uloc                               calculate the number of unique lines of code (ULOC) for the project
  -v, --verbose                            verbose output
      --version                            version for scc
  -w, --wide                               wider output with additional statistics (implies --complexity)

RESTEasyに対して実行。

$ scc
───────────────────────────────────────────────────────────────────────────────
Language                 Files     Lines   Blanks  Comments     Code Complexity
───────────────────────────────────────────────────────────────────────────────
Java                      3976    322762    44293     41051   237418      16317
XML                        225     15401     1190      1264    12947          0
AsciiDoc                    61     14414     3054         0    11360          0
Properties File             49      5746       37      4140     1569          0
Plain Text                  12       197       25         0      172          0
Python                       7       212       56        54      102          0
YAML                         7       721       54        58      609          0
Markdown                     4       260       72         0      188          0
Document Type Defin…         2         2        0         0        2          0
HTML                         2       216       34         0      182          0
SQL                          2         3        0         0        3          0
XML Schema                   2        54        3         0       51          0
Dockerfile                   1         0        0         0        0          0
JSON                         1        37        0         0       37          0
JavaScript                   1       541       54        76      411        132
Org                          1        16        0         0       16          0
Shell                        1        24        8         1       15          0
TOML                         1         5        0         0        5          0
───────────────────────────────────────────────────────────────────────────────
Total                     4355    360611    48880     46644   265087      16449
───────────────────────────────────────────────────────────────────────────────
Estimated Cost to Develop (organic) $9,465,709
Estimated Schedule Effort (organic) 32.31 months
Estimated People Required (organic) 26.03
───────────────────────────────────────────────────────────────────────────────
Processed 13426363 bytes, 13.426 megabytes (SI)
───────────────────────────────────────────────────────────────────────────────

ちょっと驚きますが、ものすごく速いです…。

よく見ると複雑度や期間、コストが出ているのも面白いですね。

1ファイルで計測。

$ scc resteasy-core/src/main/java/org/jboss/resteasy/core/SynchronousDispatcher.java
───────────────────────────────────────────────────────────────────────────────
Language                 Files     Lines   Blanks  Comments     Code Complexity
───────────────────────────────────────────────────────────────────────────────
Java                         1       554       50        75      429         50
───────────────────────────────────────────────────────────────────────────────
Total                        1       554       50        75      429         50
───────────────────────────────────────────────────────────────────────────────
Estimated Cost to Develop (organic) $11,109
Estimated Schedule Effort (organic) 2.49 months
Estimated People Required (organic) 0.40
───────────────────────────────────────────────────────────────────────────────
Processed 23518 bytes, 0.024 megabytes (SI)
───────────────────────────────────────────────────────────────────────────────

FastAPI。

$ scc
───────────────────────────────────────────────────────────────────────────────
Language                 Files     Lines   Blanks  Comments     Code Complexity
───────────────────────────────────────────────────────────────────────────────
Python                    1142     89909    11683      4299    73927       2423
Markdown                  1060    178775    74072         0   104703          0
YAML                        66      7128       83        32     7013          0
SVG                         51     12718        1         0    12717          1
Plain Text                   7        56        0         0       56          0
Shell                        4        29        7         4       18          0
CSS                          3       304       46        29      229          0
HTML                         3       105        1         0      104          0
JavaScript                   3       478       38        71      369         69
License                      1        21        4         0       17          0
TOML                         1       269       24        42      203          1
───────────────────────────────────────────────────────────────────────────────
Total                     2341    289792    85959      4477   199356       2494
───────────────────────────────────────────────────────────────────────────────
Estimated Cost to Develop (organic) $7,017,882
Estimated Schedule Effort (organic) 28.84 months
Estimated People Required (organic) 21.62
───────────────────────────────────────────────────────────────────────────────
Processed 18090919 bytes, 18.091 megabytes (SI)
───────────────────────────────────────────────────────────────────────────────

1ファイルで計測。

$ scc fastapi/applications.py
───────────────────────────────────────────────────────────────────────────────
Language                 Files     Lines   Blanks  Comments     Code Complexity
───────────────────────────────────────────────────────────────────────────────
Python                       1      4667      274      1137     3256        231
───────────────────────────────────────────────────────────────────────────────
Total                        1      4667      274      1137     3256        231
───────────────────────────────────────────────────────────────────────────────
Estimated Cost to Develop (organic) $93,306
Estimated Schedule Effort (organic) 5.58 months
Estimated People Required (organic) 1.48
───────────────────────────────────────────────────────────────────────────────
Processed 180276 bytes, 0.180 megabytes (SI)
───────────────────────────────────────────────────────────────────────────────

tokei

最後はtokeiです。こちらはUbuntu Linuxだとバイナリーをダウンロードすることになりますね。

$ curl -LO https://github.com/XAMPPRocky/tokei/releases/download/v12.1.2/tokei-x86_64-unknown-linux-gnu.tar.gz
$ tar xf tokei-x86_64-unknown-linux-gnu.tar.gz
$ cp tokei ~/.local/bin

バージョン。

$ tokei --version
tokei 12.1.2 compiled with serialization support: json, cbor, yaml

ヘルプ。

$ tokei --help
tokei 12.1.2 compiled with serialization support: json, cbor, yaml
Erin P. <xampprocky@gmail.com> + Contributors
Count your code, quickly.
Support this project on GitHub Sponsors: https://github.com/sponsors/XAMPPRocky

USAGE:
    tokei [FLAGS] [OPTIONS] [--] [input]...

FLAGS:
    -C, --compact             Do not print statistics about embedded languages.
    -f, --files               Will print out statistics on individual files.
    -h, --help                Prints help information
        --hidden              Count hidden files.
    -l, --languages           Prints out supported languages and their extensions.
        --no-ignore           Don't respect ignore files (.gitignore, .ignore, etc.). This implies --no-ignore-parent,
                              --no-ignore-dot, and --no-ignore-vcs.
        --no-ignore-dot       Don't respect .ignore and .tokeignore files, including those in parent directories.
        --no-ignore-parent    Don't respect ignore files (.gitignore, .ignore, etc.) in parent directories.
        --no-ignore-vcs       Don't respect VCS ignore files (.gitignore, .hgignore, etc.), including those in parent
                              directories.
    -V, --version             Prints version information
    -v, --verbose             Set log output level:
                                          1: to show unknown file extensions,
                                          2: reserved for future debugging,
                                          3: enable file level trace. Not recommended on multiple files

OPTIONS:
    -c, --columns <columns>                Sets a strict column width of the output, only available for terminal output.
    -e, --exclude <exclude>...             Ignore all files & directories matching the pattern.
    -i, --input <file_input>               Gives statistics from a previous tokei run. Can be given a file path, or
                                           "stdin" to read from stdin.
    -n, --num-format <num_format_style>    Format of printed numbers, i.e. plain (1234, default), commas (1,234), dots
                                           (1.234), or underscores (1_234). Cannot be used with --output. [possible
                                           values: commas, dots, plain, underscores]
    -o, --output <output>                  Outputs Tokei in a specific format. Compile with additional features for more
                                           format support. [possible values: cbor, json, yaml]
    -s, --sort <sort>                      Sort languages based on column [possible values: files, lines, blanks, code,
                                           comments]
    -t, --type <types>                     Filters output by language type, seperated by a comma. i.e. -t=Rust,Markdown

ARGS:
    <input>...    The path(s) to the file or directory to be counted.

RESTEasyで計測。

$ tokei
===============================================================================
 Language            Files        Lines         Code     Comments       Blanks
===============================================================================
 AsciiDoc               61        14414        11301           59         3054
 HTML                    2          216          187            0           29
 Java                 3976       322762       237481        41025        44256
 JavaScript              1          541          455           48           38
 JSON                    1           37           37            0            0
 Org                     1           16           16            0            0
 Python                  7          212          106           50           56
 Shell                   1           24           15            1            8
 SQL                     2            3            3            0            0
 Plain Text             12          197            0          172           25
 XML                   225        15401        12951         1264         1186
-------------------------------------------------------------------------------
 Markdown                4          257            0          185           72
 |- BASH                 1            3            3            0            0
 (Total)                            260            3          185           72
===============================================================================
 Total                4294       354080       262552        42804        48724
===============================================================================

こちらも高速です。

単一ファイルで計測。

$ tokei resteasy-core/src/main/java/org/jboss/resteasy/core/SynchronousDispatcher.java
===============================================================================
 Language            Files        Lines         Code     Comments       Blanks
===============================================================================
 Java                    1          554          429           75           50
===============================================================================
 Total                   1          554          429           75           50
===============================================================================

FastAPIで計測。

$ tokei
===============================================================================
 Language            Files        Lines         Code     Comments       Blanks
===============================================================================
 CSS                     3          304          229           29           46
 HTML                    3          105          104            0            1
 JavaScript              3          478          369           71           38
 Python               1142        89909        77889          721        11299
 Shell                   4           29           18            4            7
 SVG                    51        12718        12717            0            1
 Plain Text              7           56            0           56            0
 TOML                    1          269          203           42           24
 YAML                   40         5722         5717            5            0
-------------------------------------------------------------------------------
 Markdown             1060       169156            0        96171        72985
 |- BASH                 9           18           16            2            0
 |- CSS                  7           14           14            0            0
 |- Dockerfile          10          277          160           24           93
 |- HTML                 6           12           12            0            0
 |- JavaScript          14           40           36            4            0
 |- JSON               250         4334         4286            0           48
 |- Markdown             1            6            0            4            2
 |- Python             357         5281         4259          124          898
 |- TOML                 6          174          138            0           36
 |- TypeScript           6            6            6            0            0
 |- YAML                 2            2            2            0            0
 (Total)                         179320         8929        96329        74062
===============================================================================
 Total                2314       278746        97246        97099        84401
===============================================================================

単一ファイルで計測。

===============================================================================
 Language            Files        Lines         Code     Comments       Blanks
===============================================================================
 Python                  1         4667         4597           23           47
===============================================================================
 Total                   1         4667         4597           23           47
===============================================================================

おわりに

ソースコードの規模を計測できるツール、cloc、scc、tokeiを試してみました。

ただ動かしただけなのですが、こういうのは1度触って覚えておくというのが大事なのかなと…。

機会があったら使ってみましょう。sccはおもしろいなと思いました。




以上の内容はhttps://kazuhira-r.hatenablog.com/entry/2025/10/04/163206より取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14