Accueil > > > CHRONOMÈTRE MESURANT LA DURÉE D'UNE FONCTION
CHRONOMÈTRE MESURANT LA DURÉE D'UNE FONCTION
Information sur la source
Description
Cette classe est un chronomètre qui vous permet de mesurer le temps que prend votre code. J'ai fait cette classe en me basant sur le framework Atlas. Il y a une classe StopWatch qui est la base du chronomètre, vous n'avez pas à utiliser directement cette classe ! La seconde classe est un singleton, vous n'avez pas besoin de l'instancier pour l'utiliser. Le fonctionnement est simple, avant de rentrer dans le code, vous démarrer un nouveau StopWatch, executer votre code et stopper le StopWatch. Cyril.Debug.Analyzers.start('Test1'); // Le code a executé Cyril.Debug.Analyzers.stop('Test1'); Une popup vous affichera le temps passer pour chacun des compteurs courant. Vous avez aussi les méthodes pause et resume qui font ce que le nom indique :-)
Source
- Type.registerNamespace('Cyril.Debug');
-
- Cyril.Debug.StopWatch = function(name, description){
- /// <summary>This class can mesure the time take by some line of code. You dont't have to manipulate this object directly, use the Cyril.Debug.Analyzers type.</summary>
- /// <param name="name" type="String" mayBeNull="false">Name of this StopWatch.</param>
- /// <param name="description" type="String" optional="true" mayBeNull="true">Description of this StopWatch.</param>
- /// <returns type="StopWatch"></returns>
- var e = Function._validateParams(arguments, [
- {name: 'name', type: String},
- {name: 'description', type: String, mayBeNull: true, optional:true}
- ]);
- if (e) throw e;
-
- /* Private fields */
- this._name = name;
- this._description = description || '';
- }
- Cyril.Debug.StopWatch.prototype = {
-
- /* Private fields" */
- _startDate : null,
- _endDate : null,
-
- /* Public accesssors */
- get_name : function(){
- return this._name;
- },
-
- get_description : function(){
- return this._description;
- },
- set_description : function(description){
- this._description = description;
- },
-
- _elapsedTime : 0,
- get_elapsedTime : function(){
- return this._elapsedTime;
- },
-
- _isStarted : false,
- get_isStarted : function(){
- return this._isStarted;
- },
- _isRunning : false,
- get_isRunning : function(){
- return this._isRunning;
- },
- _isStopped : false,
- get_isStopped : function() {
- return this._isStopped;
- },
-
-
- /* Public Methods */
-
- start : function(){
- this._isStarted = true;
- this._isRunning = true;
- this._elapsedTime = 0;
- this._startDate = new Date();
- },
- pause : function(){
- this._elapsedTime += new Date() - this._startDate;
- this._isRunning = false;
- },
- resume : function(){
- this._isStarted = true;
- this._startDate = new Date();
- },
- stop : function(){
- if(this._isRunning)
- this._elapsedTime += new Date() - this._startDate;
- this._isRunning = false;
- this._isStopped = true;
- }
- }
- Cyril.Debug.StopWatch.registerClass('Cyril.Debug.StopWatch', null);
-
- Cyril.Debug._Analyzers = function(){
- /// <summary></summary>
- }
- Cyril.Debug._Analyzers.prototype = {
-
- /* Private fields */
- _popup : null,
- _stopWatches : {},
-
- /* Public properties */
- get_StopWatch : function(name){
- /// <summary>Get a StopWatch objet by name.</summary>
- /// <param name="name" type="String" mayBeNull="false" optional="false">Name of the StopWatch.</param>
- /// <returns type="StopWatch"></returns>
- var e = Function._validateParams(arguments, [
- {name: 'name', type: String, mayBeNull: false, optional: false},
- {name: 'description', type: String, mayBeNull: true, optional:true}
- ]);
- if (e) throw e;
-
- return this._stopWatches[name];
- },
-
- /* Public methods */
-
- start : function(name, description){
- /// <summary>Start a new StopWatch, if the name already existe it will be deleted.</summary>
- /// <param name="name" type="String" mayBeNull="false" optional="false">Name of the StopWatch.</param>
- /// <param name="description" type="String" optional="true" mayBeNull="true">Description of this StopWatch.</param>
- var e = Function._validateParams(arguments, [
- {name: 'message', type: String, mayBeNull: false, optional: false}
- ]);
- if (e) throw e;
-
- this._stopWatches[name] = new Cyril.Debug.StopWatch(name, description);
- this._stopWatches[name].start();
- },
- pause : function(name){
- /// <summary>Pause the associated StopWatch, use the Resume method to restart it.</summary>
- /// <param name="name" type="String" mayBeNull="false" optional="false">Name of the StopWatch.</param>
- var e = Function._validateParams(arguments, [
- {name: 'message', type: String, mayBeNull: false, optional: false}
- ]);
- if (e) throw e;
-
- this._stopWatches[name].pause();
- },
- resume : function(name){
- /// <summary>Resume the associated StopWatch.</summary>
- /// <param name="name" type="String" mayBeNull="false" optional="false">Name of the StopWatch.</param>
- var e = Function._validateParams(arguments, [
- {name: 'message', type: String, mayBeNull: false, optional: false}
- ]);
- if (e) throw e;
-
- this._stopWatches[name].resume();
- },
- stop : function(name){
- /// <summary>Stop the associated StopWatch and display the results.</summary>
- /// <param name="name" type="String" mayBeNull="false" optional="false">Name of the StopWatch.</param>
- var e = Function._validateParams(arguments, [
- {name: 'message', type: String, mayBeNull: false, optional: false}
- ]);
- if (e) throw e;
-
- this._stopWatches[name].stop();
- this.display();
- },
- display : function(){
- /// <summary>Display all StopWatch in a popup.</summary>
- if (this._popup){
- try {
- this._popup.close();
- } catch (ex) {
- }
- }
-
- this._popup = window.open('about:blank', '_blank', 'resizable=yes,scrollbars=yes,width=300, height=200');
- if (!this._popup){
- alert('un anti popup a bloqué la fenêtre des résultats des tests');
- return;
- }
- var sb = new Sys.StringBuilder();
- sb.append('<html><head><title>Cyril.Debug.Analyzers</title></head><body><ul>');
- for (name in this._stopWatches){
- sb.append(String.format('<li><strong>{0}</strong> : {1} ms', this._stopWatches[name].get_name(), this._stopWatches[name].get_elapsedTime()));
- var description = this._stopWatches[name].get_description();
- if (description){
- sb.append(String.format('<br /><p>{0}</p>', description));
- }
- sb.append('</li>');
- }
- sb.append('</ul></body></html>');
- this._popup.document.write(sb.toString());
- }
- }
- Cyril.Debug._Analyzers.registerClass('Cyril.Debug._Analyzers', null);
-
- Cyril.Debug.Analyzers = new Cyril.Debug._Analyzers();
Type.registerNamespace('Cyril.Debug');
Cyril.Debug.StopWatch = function(name, description){
/// <summary>This class can mesure the time take by some line of code. You dont't have to manipulate this object directly, use the Cyril.Debug.Analyzers type.</summary>
/// <param name="name" type="String" mayBeNull="false">Name of this StopWatch.</param>
/// <param name="description" type="String" optional="true" mayBeNull="true">Description of this StopWatch.</param>
/// <returns type="StopWatch"></returns>
var e = Function._validateParams(arguments, [
{name: 'name', type: String},
{name: 'description', type: String, mayBeNull: true, optional:true}
]);
if (e) throw e;
/* Private fields */
this._name = name;
this._description = description || '';
}
Cyril.Debug.StopWatch.prototype = {
/* Private fields" */
_startDate : null,
_endDate : null,
/* Public accesssors */
get_name : function(){
return this._name;
},
get_description : function(){
return this._description;
},
set_description : function(description){
this._description = description;
},
_elapsedTime : 0,
get_elapsedTime : function(){
return this._elapsedTime;
},
_isStarted : false,
get_isStarted : function(){
return this._isStarted;
},
_isRunning : false,
get_isRunning : function(){
return this._isRunning;
},
_isStopped : false,
get_isStopped : function() {
return this._isStopped;
},
/* Public Methods */
start : function(){
this._isStarted = true;
this._isRunning = true;
this._elapsedTime = 0;
this._startDate = new Date();
},
pause : function(){
this._elapsedTime += new Date() - this._startDate;
this._isRunning = false;
},
resume : function(){
this._isStarted = true;
this._startDate = new Date();
},
stop : function(){
if(this._isRunning)
this._elapsedTime += new Date() - this._startDate;
this._isRunning = false;
this._isStopped = true;
}
}
Cyril.Debug.StopWatch.registerClass('Cyril.Debug.StopWatch', null);
Cyril.Debug._Analyzers = function(){
/// <summary></summary>
}
Cyril.Debug._Analyzers.prototype = {
/* Private fields */
_popup : null,
_stopWatches : {},
/* Public properties */
get_StopWatch : function(name){
/// <summary>Get a StopWatch objet by name.</summary>
/// <param name="name" type="String" mayBeNull="false" optional="false">Name of the StopWatch.</param>
/// <returns type="StopWatch"></returns>
var e = Function._validateParams(arguments, [
{name: 'name', type: String, mayBeNull: false, optional: false},
{name: 'description', type: String, mayBeNull: true, optional:true}
]);
if (e) throw e;
return this._stopWatches[name];
},
/* Public methods */
start : function(name, description){
/// <summary>Start a new StopWatch, if the name already existe it will be deleted.</summary>
/// <param name="name" type="String" mayBeNull="false" optional="false">Name of the StopWatch.</param>
/// <param name="description" type="String" optional="true" mayBeNull="true">Description of this StopWatch.</param>
var e = Function._validateParams(arguments, [
{name: 'message', type: String, mayBeNull: false, optional: false}
]);
if (e) throw e;
this._stopWatches[name] = new Cyril.Debug.StopWatch(name, description);
this._stopWatches[name].start();
},
pause : function(name){
/// <summary>Pause the associated StopWatch, use the Resume method to restart it.</summary>
/// <param name="name" type="String" mayBeNull="false" optional="false">Name of the StopWatch.</param>
var e = Function._validateParams(arguments, [
{name: 'message', type: String, mayBeNull: false, optional: false}
]);
if (e) throw e;
this._stopWatches[name].pause();
},
resume : function(name){
/// <summary>Resume the associated StopWatch.</summary>
/// <param name="name" type="String" mayBeNull="false" optional="false">Name of the StopWatch.</param>
var e = Function._validateParams(arguments, [
{name: 'message', type: String, mayBeNull: false, optional: false}
]);
if (e) throw e;
this._stopWatches[name].resume();
},
stop : function(name){
/// <summary>Stop the associated StopWatch and display the results.</summary>
/// <param name="name" type="String" mayBeNull="false" optional="false">Name of the StopWatch.</param>
var e = Function._validateParams(arguments, [
{name: 'message', type: String, mayBeNull: false, optional: false}
]);
if (e) throw e;
this._stopWatches[name].stop();
this.display();
},
display : function(){
/// <summary>Display all StopWatch in a popup.</summary>
if (this._popup){
try {
this._popup.close();
} catch (ex) {
}
}
this._popup = window.open('about:blank', '_blank', 'resizable=yes,scrollbars=yes,width=300, height=200');
if (!this._popup){
alert('un anti popup a bloqué la fenêtre des résultats des tests');
return;
}
var sb = new Sys.StringBuilder();
sb.append('<html><head><title>Cyril.Debug.Analyzers</title></head><body><ul>');
for (name in this._stopWatches){
sb.append(String.format('<li><strong>{0}</strong> : {1} ms', this._stopWatches[name].get_name(), this._stopWatches[name].get_elapsedTime()));
var description = this._stopWatches[name].get_description();
if (description){
sb.append(String.format('<br /><p>{0}</p>', description));
}
sb.append('</li>');
}
sb.append('</ul></body></html>');
this._popup.document.write(sb.toString());
}
}
Cyril.Debug._Analyzers.registerClass('Cyril.Debug._Analyzers', null);
Cyril.Debug.Analyzers = new Cyril.Debug._Analyzers();
Conclusion
Vous trouverez dans le zip un exemple d'utilisation de ce chronomètre, pour pouvoir l'executer il vous faut la beta 1 d'Atlas (http://ajax.asp.net) et Visual Web Dev Express (gratuit : http://www.microsoft.com/france/msdn/vstudio/expre ss/vwd/telechargez.mspx)
Si vous ne voulez pas utiliser ASP.net pour tester le script il vous faudra utiliser Microsoft Ajax Library pour faire fonctionner le fichier JavaScript.
Historique
- 23 octobre 2006 14:02:09 :
- .
Sources du même auteur
Sources de la même categorie
Commentaires et avis
Discussions en rapport avec ce code source dans le forum
Recherche compteur donnant le temps qui c'est passer depuis une date. [ par vali103 ]
Bonjour,Je recherche un compteur permettant de donner le temps à partir d'une date (en année, mois, jours, heures, minutes, et pourquoi pas secondes)e
Compteur de temps restant pour acheter un produit [ par jzeel ]
Bonjour a tous, J'ai un site ou je vends des articles rabais, mais l'article est disponible pour achat que pendant 48 heures... j'aimerais mettre un
Compteur de temps restant Javascript [ par poinball ]
Bonjour à tous ! J'ai réussi a me trouver un petit code sympa pour me faire un countdown d'évènement. Maintenant j'aimerais sortir du countdown séparé
Script enchainer plusieur image en fondu compatibilité IE et Safari [ par jakado ]
Bonjour j'ai le code qui suit (que j'ai trouvé sur un blog) qui marche très bien sur IE et Firefox mais pas sur Safari et Chrome je serai content si
transition fond ecran éléatoire [ par tif27940 ]
Bonjour, voici mon code pour l'affiche de mon fond écran aléatoire qui fonctionne très bien mais je voudrais savoir si il est possible de mettre des
Mélanger les lettres d'un mot [ par Lany09 ]
L'exercice en javascript consiste à mettre en place une application web capable de mélanger les lettres d'un mot. ça parait peut-être tout con mais c'
Temps d'exécution [ par Bobshit75 ]
Bonjour à tous, Voilà, j'ai essayé de faire de l'Ajax sans JQuery (je regrette mais bon ^^). Le problème que je rencontre c'est un problème au niveau
voyage dans le temps par google [ par xentrikweb ]
recherche du caractère &é sur google donne 0.00 seconde et de surcroit il pointe du javascripte sur code source.fr si cette entiter vie elle cherche u
Bouger deux ScrollBar en même temps [ par BeberEberlue ]
Bonjour Je cherche à faire en sorte que mes deux scrollbar horizontales bougent en même temps, c-a-d qui si l'utilisateur déplace la première scrollb
afficher deux div en même temps dans deux cellules différentes d'un tableau [ par lyamcarter ]
Bonjour, Je souhaite afficher plusieur div en même temps dans différente cellule de tableau. Mais cela ne fonctionne pas. pourriez vous m'apporter vot
|
Derniers Blogs
XNA IS DEAD!XNA IS DEAD! par richardc
Depuis la semaine dernière (et grâce aux TechDays 2012), je me penche activement sur la nouvelle version de Windows, aka Windows 8. Vous me direz, il était temps puisque la première preview date de Septembre dernier.
OK. Remarquez, on n'en est qu'aux...
Cliquez pour lire la suite de l'article par richardc TECHDAYS PARIS 2012 : WINDOWS SERVER "8" QUOI DE 9 !TECHDAYS PARIS 2012 : WINDOWS SERVER "8" QUOI DE 9 ! par ROMELARD Fabrice
Speakers: Fabrice Meillon et Stanislas Quastana Cette session est basée entièrement sur celle donnée lors de la BUILD cet hiver. Il n'y a pas d'ajout d'information en rapport avec cet évènement passé. Windows 8 Server sera intégralem...
Cliquez pour lire la suite de l'article par ROMELARD Fabrice [HTML5] AUTOUR DU W3C : NOUVEAUX STANDARDS ET WEB MOBILE (LILLE)[HTML5] AUTOUR DU W3C : NOUVEAUX STANDARDS ET WEB MOBILE (LILLE) par Gio
Je m'y prends un peu tard je sais, mais bon je suis développeur web et donc hyper fainéant ! Toujours dans le cadre des technologies émergentes, ici HTML5, parce qu'on aime HTML5 chez Wyg , nous seront présent, le vieux ( Aurélien V.) et moi, pour pr...
Cliquez pour lire la suite de l'article par Gio [WP7] DYNAMICALLY CHANGE STARTUP PAGE[WP7] DYNAMICALLY CHANGE STARTUP PAGE par KooKiz
Let's say that you want to allow the user to customize the startup page of your application. You can easily change the startup page by editing the 'NavigationPage' attribute in the manifest file. But the manifest cannot be modified once the applicatio...
Cliquez pour lire la suite de l'article par KooKiz
Logiciels
DocTranslate (V3.1.0.0)DOCTRANSLATE (V3.1.0.0)DocTranslate est un traducteur de document Microsoft Word, PowerPoint et Excel. Il permet d'autom... Cliquez pour télécharger DocTranslate Tribler (2012)TRIBLER (2012)Tribler est un client pair à pair (P2P/Peer-to-Peer) open source avec la capacité de regarder des... Cliquez pour télécharger Tribler OneSwarm (2012)ONESWARM (2012)Le peer-to-peer qui protège votre vie privée, c'est OneSwarm.
Ce logiciel de peer-to-peer crypté... Cliquez pour télécharger OneSwarm PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V8.4)PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO (V8.4)PONAMEDIA TV DEVIENS HELLLOOO FLASH
LA TV SUR VOTRE ORDINATEUR.
Toute une plateforme Multi... Cliquez pour télécharger PONAMEDIA PREMIUM - HELLLOOO FLASH DEMO Academy System (17.2.1.0)ACADEMY SYSTEM (17.2.1.0)Logiciel de gestion des établissements.
- élèves/étudiants (inscription, dossier, absence...)
-... Cliquez pour télécharger Academy System
|