Ai Dreams Forum

Member's Experiments & Projects => AI Programming => Topic started by: yotamarker on May 20, 2020, 09:06:17 pm

Title: java CALOC
Post by: yotamarker on May 20, 2020, 09:06:17 pm
https://www.yotamarker.com/t297-java-cloc-count-active-lines-of-code-in-a-method

is there already a cleaner way to count active lines of code in a function ? this works but it is kinda crude
but it works ! with this the efficiency of a function can be measured :)
tbh ngl tbpqh tbhngl ngltbh
rabalabadabdab !!!
Title: Re: java CALOC
Post by: frankinstien on May 20, 2020, 09:47:39 pm
You might want to look at this: https://docs.microsoft.com/en-us/visualstudio/code-quality/code-metrics-values?view=vs-2019 (https://docs.microsoft.com/en-us/visualstudio/code-quality/code-metrics-values?view=vs-2019)

Visual Studio does work with other languages other than .NET supported languages. But there is a listing on that page that could direct you to other types of metrics for code.
Title: Re: java CALOC
Post by: Don Patrick on May 21, 2020, 01:13:52 pm
I am puzzled about the practical use of activated code lines as a measure of efficiency. Normally I would tally clock ticks per second if I wanted to know if a function needs optimising. A code line could contain anything from a value assignment to a function call in a loop, so there can be quite a bit of difference in their execution times.
Title: Re: java CALOC
Post by: yotamarker on May 21, 2020, 02:22:58 pm
I am puzzled about the practical use of activated code lines as a measure of efficiency. Normally I would tally clock ticks per second if I wanted to know if a function needs optimising. A code line could contain anything from a value assignment to a function call in a loop, so there can be quite a bit of difference in their execution times.
that is the thing I didn't find other ways to do it.
Title: Re: java CALOC
Post by: Don Patrick on May 21, 2020, 04:51:11 pm
I see. I'm not very familiar with Java, but some of the functions here look promising:
https://www.tutorialspoint.com/javatime/javatime_clock.htm

In C++, you'd call a clock() function at the start of the function, another one at the end, and compare the difference, like a stopwatch. Or more commonly you start a clock first, then run your function 1000x, then check the difference.
Title: Re: java CALOC
Post by: frankinstien on May 21, 2020, 05:14:05 pm
In C++, you'd call a clock() function at the start of the function, another one at the end, and compare the difference, like a stopwatch. Or more commonly you start a clock first, then run your function 1000x, then check the difference.

What would work a bit better, and perhaps this is what you implied, set up a test rig or harness that links or references the function library(s) that you want to test, and before you call each function start a stopwatch like timer.  Collect the results and store them in a file or just output to a console or UI window.
Title: Re: java CALOC
Post by: yotamarker on May 24, 2020, 02:34:09 am
I see. I'm not very familiar with Java, but some of the functions here look promising:
https://www.tutorialspoint.com/javatime/javatime_clock.htm

In C++, you'd call a clock() function at the start of the function, another one at the end, and compare the difference, like a stopwatch. Or more commonly you start a clock first, then run your function 1000x, then check the difference.

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());