Java - χρονομέτρηση προγράμματος

Συζητήσεις για γλώσσες προγραμματισμού και θέματα σχετικά με προγραμματισμό.
Post Reply
User avatar
semika
Mbyte level
Mbyte level
Posts: 998
Joined: Sat May 01, 2004 2:21 pm
Location: Αθήνα - Β.Π.

Java - χρονομέτρηση προγράμματος

Post by semika » Wed Apr 20, 2005 11:40 pm

Υπάρχει κάποια εντολή στη ξαωα που χρονομετρά το πρόγραμμά που έχεις γράψει??

Ευχαριστώ :smile:
User avatar
Einherjar
Venus Project Founder
Venus Project Founder
Posts: 3751
Joined: Tue Jan 27, 2004 4:42 pm
Academic status: Alumnus/a
Gender:
Location: Washington DC, USA
Contact:

Post by Einherjar » Wed Apr 20, 2005 11:55 pm

Μπορείς όταν ξεκινά το πρόγραμμα να θέτεις σε μια long μεταβλητή την τιμή System.getCurrentTimeMilis() (ή κάπως έτσι). Πριν ολοκληρωθεί το πρόγραμμα θα ξανακαλέσεις τη συνάρτηση αυτή και η διαφορά των 2 τιμών θα είναι ο χρόνος εκτέλεσης του προγράμματος σε ms
[Better to understand a little than to misunderstand a lot]
User avatar
semika
Mbyte level
Mbyte level
Posts: 998
Joined: Sat May 01, 2004 2:21 pm
Location: Αθήνα - Β.Π.

Post by semika » Thu Apr 21, 2005 12:39 am

EYXAΡΙΣΤΩ ΠΟΛΥ!!!!!!!!!!! :smile: :smile: :smile:
User avatar
AmmarkoV
Wow! Terabyte level
Wow! Terabyte level
Posts: 2838
Joined: Thu Nov 04, 2004 2:55 pm
Gender:
Location: Reloaded @ Santa Friday
Contact:

Post by AmmarkoV » Thu Apr 21, 2005 12:40 am

Βασικά αυτό που θα κάνει μάλλον το System.getCurrentTimeMilis() που λές Einherjar είναι ένα call στο GetTickCount του Win32API.. :???:
--/ Και μετά λέμε γιατί αργεί η JVM :cool: /--
( The GetTickCount function retrieves the number of milliseconds that have elapsed since Windows was started.
DWORD GetTickCount(VOID))
Spoiler: εμφάνιση/απόκρυψη
I would love to change the world, but they won't give me the source code. Οι καθηγητές πληρώνονται από το δημόσιο αρα από όλους τους Έλληνες για να κάνουν τα μαθήματα. Όλοι οι Έλληνες θα έπρεπε να μπορούν να δουν τα μαθήματα τα οποία πληρώνουν! Tο πνευματικό έργο που επιτελείται με τα χρήματα του δημοσίου ΔΕΝ είναι μόνο δικό σας Όποιος δεν δίνει πανελλήνιες έχει δικαίωμα στην γνώση που πληρώνει [url=http://ammar.gr/gddg]gddg blog[/url]
Image
User avatar
Einherjar
Venus Project Founder
Venus Project Founder
Posts: 3751
Joined: Tue Jan 27, 2004 4:42 pm
Academic status: Alumnus/a
Gender:
Location: Washington DC, USA
Contact:

Post by Einherjar » Thu Apr 21, 2005 12:45 am

Παρακαλώ semika.

ammarkov δε νομίζω να ισχύει κατ'ανάγκη αυτό. Η συνάρτηση αυτή επιστρέφει τα miliseconds από την 1/1/74. Δε βρίσκω κάποιον ουσιαστικό λόγο να καλεί τη συνάρτηση που λες.
[Better to understand a little than to misunderstand a lot]
User avatar
AmmarkoV
Wow! Terabyte level
Wow! Terabyte level
Posts: 2838
Joined: Thu Nov 04, 2004 2:55 pm
Gender:
Location: Reloaded @ Santa Friday
Contact:

Post by AmmarkoV » Thu Apr 21, 2005 12:49 am

Έτσι από περιέργεια :-D θα φτιάξω 2 προγραμματάκια και θα τα αντιπαραβάλλω..
Μπορεί να έχω άδικο..

ADDED
Δεν γίνεται compile (!)
cannot resolve symbol..
symbol : method getCurrentTimeMilis ()
location: class java.lang.System
long time1=System.getCurrentTimeMilis();
^
1 error

Μάλλον δεν είναι έτσι ακριβώς η εντολή ή δεν έχω κάνει κάποιο package import..

Πάντως υπάρχει και 2η εντολή στο ίδιο το Win32API που λέγεται "GetCurrentTime" και κάνει την ίδια δουλειά με την GetTickCount

The GetCurrentTime function retrieves the number of milliseconds that have elapsed since Windows was started.
DWORD GetCurrentTime(VOID)
Parameters
This function has no parameters.
Return Value
If the function succeeds, the return value is the number of milliseconds that have elapsed since Windows was started.
Remarks
The GetCurrentTime function is identical to the GetTickCount function. Applications should use the GetTickCount function because its name matches more closely what the function does.
The GetCurrentTime and GetMessageTime functions return different times. GetMessageTime returns the Windows time when the given message was created, not the current Windows time.
The internal timer wraps around to zero if Windows is run continuously for approximately 49.7 days

Τεσπα το παρατάω , όρεξη που έχω και εγώ βραδιάτικα.. :-D
Spoiler: εμφάνιση/απόκρυψη
I would love to change the world, but they won't give me the source code. Οι καθηγητές πληρώνονται από το δημόσιο αρα από όλους τους Έλληνες για να κάνουν τα μαθήματα. Όλοι οι Έλληνες θα έπρεπε να μπορούν να δουν τα μαθήματα τα οποία πληρώνουν! Tο πνευματικό έργο που επιτελείται με τα χρήματα του δημοσίου ΔΕΝ είναι μόνο δικό σας Όποιος δεν δίνει πανελλήνιες έχει δικαίωμα στην γνώση που πληρώνει [url=http://ammar.gr/gddg]gddg blog[/url]
Image
User avatar
eso_etoimos
bit level
bit level
Posts: 25
Joined: Sat May 01, 2004 12:02 am

Post by eso_etoimos » Thu Apr 21, 2005 2:17 pm

----Semika----
Η συνάρτηση αυτή είναι η System.CurrentTimeMilis() και πρέπει να κάνεις import java.lang.system (ή κάπως έτσι τέλος πάντων).
Bασικά αυτό που λέει ο νεκτάριος τρέχει κανονικά...

ΥΓ-> Σόρυ που μπήκα με το nick του δημήτρη αλλά είμαι στον υπολογιστή του
----Semika----
User avatar
Einherjar
Venus Project Founder
Venus Project Founder
Posts: 3751
Joined: Tue Jan 27, 2004 4:42 pm
Academic status: Alumnus/a
Gender:
Location: Washington DC, USA
Contact:

Post by Einherjar » Thu Apr 21, 2005 2:24 pm

καλά... το java.lang δεν χρειάζεται import. το κάνει αυτόματα η Java.
[Better to understand a little than to misunderstand a lot]
User avatar
AnINffected
Gbyte level
Gbyte level
Posts: 1935
Joined: Fri Jul 30, 2004 7:12 am
Location: There and Back Again

Post by AnINffected » Thu Apr 21, 2005 3:07 pm

Το κοίταξα στο api... (έχω το 1.4.2)

Το όνομα της μεθόδου είναι currentTimeMillis και όχι getCurrentTimeMilis.

Χωρίς το get μπροστά δηλαδή.

Μια και πιάσαμε αυτή τη κουβέντα ξέρετε τίποτα προγραμματάκια που να κάνουν ανάλυση πολυπλοκότητας και τα ρέστα;
The Analytical Engine has no pretensions to originate anything. It can do whatever we know how to order it to perform (...)
Ada Lovelace


Θέλω και εγώ να παίξω D&D λέμε!!! :-( :-(
User avatar
Einherjar
Venus Project Founder
Venus Project Founder
Posts: 3751
Joined: Tue Jan 27, 2004 4:42 pm
Academic status: Alumnus/a
Gender:
Location: Washington DC, USA
Contact:

Post by Einherjar » Thu Apr 21, 2005 3:24 pm

Δες στο Profiling στη Java
[Better to understand a little than to misunderstand a lot]
User avatar
Skeftomilos
bit level
bit level
Posts: 43
Joined: Fri Mar 04, 2005 8:08 am
Location: Ν.Κόσμος

Post by Skeftomilos » Fri Apr 22, 2005 2:22 am

Για τη χρονομέτρηση των build-in συναρτήσεων της VB6 είχα χρησιμοποιήσει τη συνάρτηση Timer (returns the number of seconds elapsed since midnight). Όμως η φύση της μέτρησης απαιτούσε τη συχνή κλήση της συνάρτησης αυτής, με συνέπεια τα αποτελέσματα να παραμορφώνονται λόγω του χρόνου κλήσης της συνάρτησης. Με άλλα λόγια η ίδια η Timer αποδείχτηκε αργή! Επιπλέον έδινε και τιμές μικρής ακρίβειας. Τελικά βρέθηκε ότι η GetTickCount ήταν και ακριβέστερη και πολύ ταχύτερη!

Win 98, PentiumII 300mhz

Code: Select all

Timer            (VBA) : 91.300 nsec
Now              (VBA) : 100.000 nsec
Date             (VBA) : 110.000 nsec
Time             (VBA) : 90.000 nsec
Year, Month, Day (VBA) : 3.000 nsec
DatePart         (VBA) : 15.500 nsec
DateDiff         (VBA) : 26.000 nsec
DateSerial       (VBA) : 1.500 nsec
GetTickCount (Win-API) : 160 nsec
Ξανάκανα τις δοκιμές αργότερα με Win-XP Seleron 2.4, περιμένοντας μία αναλογική μείωση των χρόνων. Τα αποτελέσματα ήταν απρόβλεπτα:

Code: Select all

Timer            (VBA) : 380 nsec
Now              (VBA) : 640 nsec
Date             (VBA) : 980 nsec
Time             (VBA) : 540 nsec
Year, Month, Day (VBA) : 700 nsec
DatePart         (VBA) : 1.200 nsec
DateDiff         (VBA) : 2.600 nsec
DateSerial       (VBA) : 265 nsec
GetTickCount (Win-API) : 27 nsec
Στον ίδιο υπολογιστή έκανα και σχετικές δοκιμές με τις συναρτήσεις χρόνου του .NET Framework 1.0:

Code: Select all

Microsoft.VisualBasic.DateAndTime.Timer  : 168 nsec
Microsoft.VisualBasic.DateAndTime.Now    : 54 nsec
Microsoft.VisualBasic.DateAndTime.Today  : 124 nsec
System.DateTime.Now                      : 50 nsec
System.DateTime.Today                    : 120 nsec
System.DateTime.Now.Ticks                : 54 nsec
System.Environment.TickCount             : 14 nsec
System.DateTime.Now.Year                 : 240 nsec
System.DateTime.Now.Month                : 250 nsec
System.DateTime.Now.Day                  : 250 nsec
System.DateTime.IsLeapYear               : 22 nsec
System.DateTime.AddDays                  : 170 nsec
System.DateTime.AddMonths                : 880 nsec
System.DateTime.AddYears                 : 880 nsec
GetTickCount                   (Win-API) : 365 nsec
Έκπληξη! Η κλήση της GetTickCount από managed κώδικα είναι πολύ πιο αργή συγκριτικά με τη VB6.

Ελπίζω όλα αυτά να είναι έστω και λίγο χρήσιμα, καθώς δεν έχουν άμεση σχέση με τη Java της $un. :)
The pure and simple truth is rarely pure and never simple. Ο μη νους δε σκέπτεται μη σκέψεις για το τίποτα.
User avatar
Tomahawk
Mbyte level
Mbyte level
Posts: 536
Joined: Tue May 18, 2004 1:50 pm

Post by Tomahawk » Fri Apr 22, 2005 9:11 pm

Απο τη κλαση System:

/**
* Returns the current value of the most precise available system
* timer, in nanoseconds.
*
* <p>This method can only be used to measure elapsed time and is
* not related to any other notion of system or wall-clock time.
* The value returned represents nanoseconds since some fixed but
* arbitrary time (perhaps in the future, so values may be
* negative). This method provides nanosecond precision, but not
* necessarily nanosecond accuracy. No guarantees are made about
* how frequently values change. Differences in successive calls
* that span greater than approximately 292 years (2<sup>63</sup>
* nanoseconds) will not accurately compute elapsed time due to
* numerical overflow.
*
* <p> For example, to measure how long some code takes to execute:
* <pre>
* long startTime = System.nanoTime();
* // ... the code being measured ...
* long estimatedTime = System.nanoTime() - startTime;
* </pre>
*
* @return The current value of the system timer, in nanoseconds.
* @since 1.5
*/
public static native long nanoTime();
User avatar
Einherjar
Venus Project Founder
Venus Project Founder
Posts: 3751
Joined: Tue Jan 27, 2004 4:42 pm
Academic status: Alumnus/a
Gender:
Location: Washington DC, USA
Contact:

Post by Einherjar » Fri Apr 22, 2005 10:55 pm

ενδιαφέρον πράγματι! δεν την είχα υπόψιν μου την κλήση αυτή
[Better to understand a little than to misunderstand a lot]
User avatar
Tomahawk
Mbyte level
Mbyte level
Posts: 536
Joined: Tue May 18, 2004 1:50 pm

Post by Tomahawk » Sat Apr 23, 2005 1:15 pm

Ναι και εμενα μου αρεσε full.Εψαχνα για τις δομες που καναμε τον shakeSort να βαλω και ενα println ετσι για τα τυπικα που να τυπωνει τον χρονο εκτελεσης και εβαζα την currentTimeInMillis αλλα η διαφορα(endTime-startTime) ηταν παντα μηδεν (προφανως :P).Και καθως εριχνα μια ματια σε ολες τις βασικες κλασσης της java στο src.zip τσοουουουπ επεταχτηκε και αυτη χεχε.

Ασχετο: Νεκταριουμ θελω καποια στιγμη συμπυκνωμενη γνωση περι J2EE!!Εχει πολυ πραγμα το βασσαλιδιο και μια συζητησουλα μαζι θα μου γλυτωσει πολυ πολυ χρονο ως γνωστο τσακαλακι επι του θεματος που εισαι :P
User avatar
Einherjar
Venus Project Founder
Venus Project Founder
Posts: 3751
Joined: Tue Jan 27, 2004 4:42 pm
Academic status: Alumnus/a
Gender:
Location: Washington DC, USA
Contact:

Post by Einherjar » Sat Apr 23, 2005 3:19 pm

θα το μοντάρουμε ;-)
[Better to understand a little than to misunderstand a lot]
User avatar
semika
Mbyte level
Mbyte level
Posts: 998
Joined: Sat May 01, 2004 2:21 pm
Location: Αθήνα - Β.Π.

Post by semika » Sat Apr 23, 2005 3:52 pm

Ευχαριστώ φίλτατε Tomahawk. αυτή δουλεύει τζιτζί!!!!

:smile: :smile:
User avatar
HdkiLLeR
Venus Project Founder
Venus Project Founder
Posts: 4356
Joined: Tue Jan 27, 2004 4:41 pm
Academic status: Alumnus/a
Gender:
Location: New York, NY
Contact:

Post by HdkiLLeR » Sat Apr 23, 2005 7:38 pm

Εαν έχεις linux box τρέξε time <prog>

Θα πάρεις κάτι τέτοιο:

Code: Select all

real    0m0.008s
user    0m0.003s
sys     0m0.005s
real = user + sys

real = συνολικός χρόνος.
user = χρόνος εκτέλεσης εντολών σε user space.
sys = χρόνος εκτέλεσης των system calls.

Είναι απο το OS καταυθείαν απο το process acounting του kernel αλλά δεν το έχω κάνει για java ποτέ για να δώ αποτελέσματα...τώρα που το λές θα το τσεκάρω :)
-----BEGIN GEEK CODE BLOCK-----
Version: 3.12
GCS d-->--- s+:+ a- C++(+++) BILS++++$ P--- L++++>+++++ E--- W+++ N+ o+ K w--
O M+ V-- PS++>+++ PE- Y++ PGP++ t+ 5+ X+ R* tv b++ DI- D+ G+++ e+++>++++ h r++ y++
------END GEEK CODE BLOCK------

"UNIX is basically a simple operating system, but you have to be a genius to understand the simplicity." -- Dennis Ritchie
Post Reply

Return to “Προγραμματισμός”