src 与构建树时间戳比较如何更快?

发布于 2024-08-14 07:33:35 字数 621 浏览 5 评论 0原文

for n in `cd src; find . -name "*.java"; cd -`;
  do a=`echo $n | cut -d '.' -f2`;
  if [[ src/$a.java -nt build/$a.class ]];
    then echo src/$a.java;
  fi;
done

它列出了src树中的所有java文件;然后,对于每一个,它都会删除后缀“.java”(cut -d '.' -f2 因为 find . 输出以 . 为前缀>)。然后使用 -nt 来测试 src 树中的 java 文件是否比构建树中相应的类文件新;如果较新,则输出。 [javac 然后可以使用这个来只编译需要的 src 文件,而不是使用 antmake]

问题是太慢了,大约需要 275ms。如何让它更快?

或者有没有更快的方法在 bash 中做到这一点?我不知道如何使用 finddiffrsyncmake (这不似乎自动遍历树,并且需要明确列出的源文件)。

for n in `cd src; find . -name "*.java"; cd -`;
  do a=`echo $n | cut -d '.' -f2`;
  if [[ src/$a.java -nt build/$a.class ]];
    then echo src/$a.java;
  fi;
done

It lists all the java files in the src tree; then for each one, it removes the suffix ".java" (cut -d '.' -f2 because find . output is prefixed with .). It then uses -nt to test if the java file in the src tree is newer than the corresponding class file in the build tree; if it is newer, it is output. [javac can then use this to compile only the needed src files, instead of using ant or make]

The problem is that it is too slow, taking about 275ms. How to make it faster?

Or is there a faster way to do this in bash? I can't see how to do it with find, diff, rsync nor make (which doesn't seem to traverse trees automatically, and needs explicitly listed source files).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

甜`诱少女 2024-08-21 07:33:35

试试这个:

for f in src/*.java; do 
  file=`basename $f`; 
  if [[ src/$file -nt build/${file//.java/} ]]; then 
     echo src/$file; 
  fi; 
done 

try this:

for f in src/*.java; do 
  file=`basename $f`; 
  if [[ src/$file -nt build/${file//.java/} ]]; then 
     echo src/$file; 
  fi; 
done 
弥枳 2024-08-21 07:33:35

我不知道这种结构是否会更快,但可能值得一试:

while read n
do
     # capture the basename all at once
     [[ $n =~ src/(.*)\.java ]]   # escape the parentheses for Bash < 3.2 (or thereabouts)
     [[ $n -nt "build/${BASH_REMATCH[1]}.class" ]] && echo $n
done < <(find src -name "*.java")

while 形式可能不会提供任何提升。折叠的if 可以。正则表达式可能会提供一点贡献。

正如您所发现的,消除对 cut 的调用产生了最大的区别。

I don't know if this structure would be any faster, but it might be worth a try:

while read n
do
     # capture the basename all at once
     [[ $n =~ src/(.*)\.java ]]   # escape the parentheses for Bash < 3.2 (or thereabouts)
     [[ $n -nt "build/${BASH_REMATCH[1]}.class" ]] && echo $n
done < <(find src -name "*.java")

The while form probably won't provide any boost. The collapsed if may. The regex might provide a small contribution.

As you've found out, eliminating a call to cut has made the biggest difference.

后知后觉 2024-08-21 07:33:35

ant 执行智能构建逻辑,除非修改了源代码,否则它不会构建类。

您可能还想研究 scons,它不是本机 Java 构建工具,但在编译高效的复杂构建树方面非常方便,并且也有 java 构建器。

ant performs smart building logic, it will not build a class unless the source was modified.

You might also like to look into scons which is not a native Java build tool but is very handy at compiling efficient complex build trees and does have java builder as well.

宣告ˉ结束 2024-08-21 07:33:35

采用kon的文件名修改方法,速度平均从320ms提高到45ms。谢谢康!

for n in `cd src; find . -name "*.java"; cd -`; do
  if [[ src/$n -nt build/${n/.java/.class} ]];
     then echo src/$n;
  fi;
done

原来的现在有点慢(以前是 275 毫秒;现在是 320 毫秒);我不知道为什么。我正在使用同一条线。播放视频后可能会出现不同的系统源。

编辑第一个评论:将“src/”删除,而不是cd src; ...cd -;。请注意,同时使用了 $n$n2 [您不能嵌套 ${var/A/B} 构造,可以吗?]

for n in `find src -name "*.java"`; do
  n2=${n/src/}
  if [[ $n -nt build/${n2/.java/.class} ]];
     then echo $n;
  fi;
done

Adopting kon's approach to the filename munging, the speed improves from 320ms to 45ms on average. Thanks kon!

for n in `cd src; find . -name "*.java"; cd -`; do
  if [[ src/$n -nt build/${n/.java/.class} ]];
     then echo src/$n;
  fi;
done

The original is a bit slower now (was 275ms; now 320ms); I don't know why. I'm using the same line. Maybe different system sources after playing a video.

EDIT re rst's comment: mangling the "src/" away instead of cd src; ... cd -;. Note that both $n and $n2 are used [you can't nest the ${var/A/B} construct, can you?]

for n in `find src -name "*.java"`; do
  n2=${n/src/}
  if [[ $n -nt build/${n2/.java/.class} ]];
     then echo $n;
  fi;
done
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文