kurosen codding


Posts : 291 Join date : 2012-04-17
 | Subject: java CLOC count active lines of code in a method Wed May 20, 2020 10:18 pm | |
| - Code:
-
package chobit;
public class CALOC { /* * this class counts how many active lines of code has run in a function 1 : use * the getCLOCVer with the function you want to test as a string parameter 2 * paste the resulting code into this class, and delete errors with the code * activeLinesOfCode++; only where it errors tho 3 if the tested function has * private methodes used inside, extend the CALOC class with the original class * containing the tested function. 4 run the pasted function via an instance of * this class : caloc.testFunction(params); 5 caloc.getActiveLinesOfCode to see * how many active line of code for the parameters you have sent */ private int activeLinesOfCode = 0;
public int getActiveLinesOfCode() { return activeLinesOfCode; }
public void resetActiveLinesOfCode() { this.activeLinesOfCode = 0; }
public String getCALOCVer(String function) { return function.replace(";", ";activeLinesOfCode++;"); } }
the main use I see for this is to see how efficiant a method is coded.  | |
|
kurosen codding


Posts : 291 Join date : 2012-04-17
 | Subject: time based CALOC Sun May 24, 2020 10:54 am | |
| test code chunks speed : - Code:
-
package chobit;
import java.util.Calendar; import java.util.Date;
public class TimeGate { // a gate that only opens x minutes after it has been set private int pause = 1; private Date openedGate = addMinutesToJavaUtilDate(new Date(), pause); private Date checkPoint = new Date(); public TimeGate(int minutes) { super(); this.pause = minutes; } public TimeGate() { } public Boolean isClosed() { return !openedGate.before(new Date()); } public void close() { this.openedGate = addMinutesToJavaUtilDate(new Date(), pause); } public void close(int minutes) { Date now = new Date(); openedGate = addMinutesToJavaUtilDate(now, minutes); } private Date addMinutesToJavaUtilDate(Date date, int minutes) { Calendar calendar = Calendar.getInstance(); calendar.setTime(date); calendar.add(Calendar.MINUTE, minutes); return calendar.getTime(); } public void setPause(int pause) { if(pause <60 && pause >0) { this.pause = pause;} }
public void resetCheckPoint() { this.checkPoint = new Date(); }
public int givenTwoDateTimesInJava8_whenDifferentiatingInSeconds_thenWeGetTen() { Date now = new Date(); long diff = now.getTime() - this.checkPoint.getTime(); long diffSeconds = diff / 1000 % 60; // long diffMinutes = diff / (60 * 1000) % 60; // long diffHours = diff / (60 * 60 * 1000) % 24; // long diffDays = diff / (24 * 60 * 60 * 1000); // System.out.print(diffDays + " days, "); // System.out.print(diffHours + " hours, "); // System.out.print(diffMinutes + " minutes, "); // System.out.print(diffSeconds + " seconds."); return (int) diffSeconds; } }
use example: - Code:
-
TimeGate timeGate = new TimeGate(); timeGate.resetCheckPoint(); for (int i = 0; i < 10000000; i++) { System.out.println(""); } System.out.println(timeGate.givenTwoDateTimesInJava8_whenDifferentiatingInSeconds_thenWeGetTen());
 | |
|