From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Marcin Borkowski Newsgroups: gmane.emacs.help Subject: Re: Making sure I'm not checking email (or doing other things) too often Date: Sun, 06 Mar 2016 14:26:47 +0100 Message-ID: <87k2lfzqi0.fsf@mbork.pl> References: <87d1rz2ebl.fsf@mbork.pl> <87h9hayr31.fsf@web.de> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: ger.gmane.org 1457270874 10012 80.91.229.3 (6 Mar 2016 13:27:54 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sun, 6 Mar 2016 13:27:54 +0000 (UTC) Cc: help-gnu-emacs@gnu.org To: Michael Heerdegen Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Sun Mar 06 14:27:45 2016 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1acYim-0003u9-Kg for geh-help-gnu-emacs@m.gmane.org; Sun, 06 Mar 2016 14:27:44 +0100 Original-Received: from localhost ([::1]:50492 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1acYii-0004eo-ON for geh-help-gnu-emacs@m.gmane.org; Sun, 06 Mar 2016 08:27:40 -0500 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:41016) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1acYiA-0004AJ-Le for help-gnu-emacs@gnu.org; Sun, 06 Mar 2016 08:27:07 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1acYi7-0007T3-FJ for help-gnu-emacs@gnu.org; Sun, 06 Mar 2016 08:27:06 -0500 Original-Received: from mail.mojserwer.eu ([2a01:5e00:2:52::8]:59612) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1acYi7-0007S2-7e for help-gnu-emacs@gnu.org; Sun, 06 Mar 2016 08:27:03 -0500 Original-Received: from localhost (localhost [127.0.0.1]) by mail.mojserwer.eu (Postfix) with ESMTP id CAC559DA6A9; Sun, 6 Mar 2016 14:26:57 +0100 (CET) X-Virus-Scanned: Debian amavisd-new at mail.mojserwer.eu Original-Received: from mail.mojserwer.eu ([127.0.0.1]) by localhost (mail.mojserwer.eu [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id V83C5xCLqzqX; Sun, 6 Mar 2016 14:26:48 +0100 (CET) Original-Received: from localhost (unknown [109.232.24.28]) by mail.mojserwer.eu (Postfix) with ESMTPSA id E1A079DA6A7; Sun, 6 Mar 2016 14:26:47 +0100 (CET) User-agent: mu4e 0.9.13; emacs 25.1.50.6 In-reply-to: <87h9hayr31.fsf@web.de> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6.x X-Received-From: 2a01:5e00:2:52::8 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:109468 Archived-At: On 2016-02-15, at 15:40, Michael Heerdegen wrote: > Factoring out the greatest common divider would give you something like > this: > > --8<---------------cut here---------------start------------->8--- > -*- lexical-binding: t -*- > > (defun stopwatch () > "Return a fresh stopwatch. > This is a function accepting zero arguments that upon each call > will return the time difference from its last call in seconds. > When called the first time it will return nil." > (let ((last-invocation nil)) > (lambda () > (prog1 (and last-invocation > (time-to-seconds (time-subtract (current-time) last-invocation))) > (setq last-invocation (current-time)))))) > --8<---------------cut-here---------------end--------------->8--- > > With that, one could make your advice functions reference and use such a > stopwatch object (each an individual one, of course). Thanks. This is my version: (defun create-stopwatch () "Return a closure which returns time (in seconds) since its last invocation every time it is called." (let (last-invocation-time (current-time)) (lambda () (prog1 (time-to-seconds (time-subtract (current-time) last-invocation-time)) (setq last-invocation-time (current-time)))))) I don't claim it to be better, but I noticed a strange phenomenon. When I invoke the "stopwatch object" (closure, in fact) for the first time, I get negative result. Why is that so? > Regards, > > Michael. Best, -- Marcin Borkowski http://octd.wmi.amu.edu.pl/en/Marcin_Borkowski Faculty of Mathematics and Computer Science Adam Mickiewicz University