-4

I run an example using the following command.

java -cp bin examples.generators.SimpleCircuitGenerator

This code outputs the time of running the SimpleCircuitGenerator. I wonder how to run the same code multiple times. How can I do it using the terminal? My goal is to measure the time of running the SimpleCircuitGenerator multiple times.

Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
Mohamed
  • 129
  • 6
  • 1
    that is not really [java] specific more like operational system stuff, or do you want to program that in java? – user85421 Apr 02 '19 at 15:37
  • 1
    Yes. I want it in java – Mohamed Apr 02 '19 at 15:38
  • If you want a program to do it, then you'll need to write a program. Otherwise, it is a bit O/S dependent. On Linux (or Windows using Cygwin), I'd use a `for` loop and `seq`. – KevinO Apr 02 '19 at 15:38
  • then you are not going to run `java -cp ... ` multiple times. To repeat a code use `for` loop, to register its time use `System.nanoTime()` or better, use some framework like JMH (not sure how that is `from the terminal`) – user85421 Apr 02 '19 at 15:39
  • Write a loop in Java and use java processbuilder. – Steve Smith Apr 02 '19 at 15:40
  • One could write [Java Single-Source program](https://dzone.com/articles/launch-single-file-source-code-programs-in-jdk-11) I suppose... – Boris the Spider Apr 02 '19 at 15:47
  • Possible duplicate of [bash shell script for 100 iteration and log time taken](https://stackoverflow.com/questions/3723818/bash-shell-script-for-100-iteration-and-log-time-taken) – Benjamin W. Apr 02 '19 at 16:57

6 Answers6

1

You can do so by using a for loop in bash : https://www.cyberciti.biz/faq/bsd-appleosx-linux-bash-shell-run-command-n-times/

1
FOR %A IN (1 2 3 4 5) DO java -cp bin examples.generators.SimpleCircuitGenerator
Saige Zhang
  • 737
  • 1
  • 7
  • 18
0

You can use a simple bash loop for something like this:
for i in {1..10}; do java -cp bin examples.generators.SimpleCircuitGenerator; done.
This may have more information for you: How to run a command multiple times, using bash shell?

nldoty
  • 471
  • 3
  • 8
0

You can repeat terminal commands as mentioned in the following link.

http://www.mactricksandtips.com/2012/02/loop-repeat-terminal-commands.html

But I suggest you add a for loop and get the number of repetitions as an argument. So you can run the command the number of times you need.

LeoN
  • 1,590
  • 1
  • 11
  • 19
0

I think the fastest solution is for you to run it throug a script. I'm assumming you use a Linux based terminal. Open a file and type:

#!/bin/bash
# Basic until loop
counter=1
until [ $counter -gt 10 ]
do
java -cp bin examples.generators.SimpleCircuitGenerator
((counter++))
done

In this case, it would run up to 10 times. You can modify it as you wish. Save the file as name_of_file.sh and run it.

AliAs
  • 93
  • 1
  • 1
  • 12
0

Create a bash file example.sh, put the following lines in it and run it ./example.sh . It will run 10 times and print time for each run. Also check out Print execution time of a shell command

#!/bin/sh

for i in {1..10}
do
    java -cp bin examples.generators.SimpleCircuitGenerator 
done