From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp2 ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms11 with LMTPS id qHfGD/sN+V7JcQAA0tVLHw (envelope-from ) for ; Sun, 28 Jun 2020 21:39:07 +0000 Received: from aspmx1.migadu.com ([2001:41d0:2:4a6f::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp2 with LMTPS id mHytC/sN+V5pFwAAB5/wlQ (envelope-from ) for ; Sun, 28 Jun 2020 21:39:07 +0000 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 839499400C1 for ; Sun, 28 Jun 2020 21:39:06 +0000 (UTC) Received: from localhost ([::1]:56044 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jpf0z-00073K-GA for larch@yhetil.org; Sun, 28 Jun 2020 17:39:05 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:57178) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jpf0K-00073D-EC for emacs-orgmode@gnu.org; Sun, 28 Jun 2020 17:38:24 -0400 Received: from latitanza.investici.org ([82.94.249.234]:37231) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jpf0G-0006ST-VN for emacs-orgmode@gnu.org; Sun, 28 Jun 2020 17:38:24 -0400 Received: from mx3.investici.org (unknown [127.0.0.1]) by latitanza.investici.org (Postfix) with ESMTP id C51CC120179; Sun, 28 Jun 2020 21:38:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=anche.no; s=stigmate; t=1593380297; bh=8UjuP3byTCcIgidxO2Q0w47Qo7wLKeMV7JC9RpEKQns=; h=To:From:Subject:Date:From; b=gD0LId0fFVR+HktiW3flkThB7oVj5xVrG2w4VEpYjF+lBpfR2WYq5qyIcrS5wXRg2 gxe6bLq/ZARnTxfayMJi3Tl41o/eSGH2slpkE5MPUvef2SPSUTSK4iojU4gSvVtIXD mcvMYKcEUMTDGQs/vNz4rxEdCw/DgTh3eYQVioRk= Received: from [82.94.249.234] (mx3.investici.org [82.94.249.234]) (Authenticated sender: mariotomo@inventati.org) by localhost (Postfix) with ESMTPSA id 8DC1912016D; Sun, 28 Jun 2020 21:38:16 +0000 (UTC) To: emacs-orgmode@gnu.org From: Mario Frasca Subject: [PATCH] partial rewrite of gnuplot-to-grid-data Message-ID: <2296ccd0-e6eb-5d0b-c5d9-c33b60932374@anche.no> Date: Sun, 28 Jun 2020 16:37:37 -0500 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.9.0 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="------------10417FBC706EF47F73D90255" Content-Language: en-US Received-SPF: pass client-ip=82.94.249.234; envelope-from=mario@anche.no; helo=latitanza.investici.org X-detected-operating-system: by eggs.gnu.org: First seen = 2020/06/28 17:38:18 X-ACL-Warn: Detected OS = Linux 2.2.x-3.x [generic] [fuzzy] X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001, URIBL_BLOCKED=0.001 autolearn=_AUTOLEARN X-Spam_action: no action X-BeenThere: emacs-orgmode@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+larch=yhetil.org@gnu.org Sender: "Emacs-orgmode" X-Scanner: scn0 Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=anche.no header.s=stigmate header.b=gD0LId0f; dmarc=none; spf=pass (aspmx1.migadu.com: domain of emacs-orgmode-bounces@gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=emacs-orgmode-bounces@gnu.org X-Spam-Score: -1.21 X-TUID: 9x/3BxjPUQk7 This is a multi-part message in MIME format. --------------10417FBC706EF47F73D90255 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit according to me, this looks better, I'm curious about the reactions from the list. in particular, I was surprised finding a duplicated evaluation of (string-to-number (nth col (nth row table))) inside a (dotimes (col num-cols) (dotimes (row num-rows) …)) --------------10417FBC706EF47F73D90255 Content-Type: text/x-patch; charset=UTF-8; name="0001-org-plot-gnuplot-to-grid-data-code-review-for-speed-.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename*0="0001-org-plot-gnuplot-to-grid-data-code-review-for-speed-.pa"; filename*1="tch" >From 9822748dfbdb99f1ea534e695e818d7533920a61 Mon Sep 17 00:00:00 2001 From: mfrasca Date: Sun, 28 Jun 2020 16:24:54 -0500 Subject: [PATCH] org-plot/gnuplot-to-grid-data: code review for speed and readability * lisp/org-plot.el (org-plot/gnuplot-to-grid-data): partial rewrite. Optimizing for speed and readability: preprocessing data in order to simplify removal on non-plotting columns; using available functions like set-difference and org-remove-by-index; loop over elements instead of counting indices and invoking `nth' (in a double loop). --- lisp/org-plot.el | 81 +++++++++++++++++++++--------------------------- 1 file changed, 36 insertions(+), 45 deletions(-) diff --git a/lisp/org-plot.el b/lisp/org-plot.el index bf81d3c37..38749c069 100644 --- a/lisp/org-plot.el +++ b/lisp/org-plot.el @@ -130,53 +130,44 @@ Pass PARAMS through to `orgtbl-to-generic' when exporting TABLE." (defun org-plot/gnuplot-to-grid-data (table data-file params) "Export the data in TABLE to DATA-FILE for gnuplot. This means in a format appropriate for grid plotting by gnuplot. -PARAMS specifies which columns of TABLE should be plotted as independent -and dependent variables." - (interactive) - (let* ((ind (- (plist-get params :ind) 1)) - (deps (if (plist-member params :deps) - (mapcar (lambda (val) (- val 1)) (plist-get params :deps)) - (let (collector) - (dotimes (col (length (nth 0 table))) - (setf collector (cons col collector))) - collector))) - (counter 0) - row-vals) - (when (>= ind 0) ;; collect values of ind col - (setf row-vals (mapcar (lambda (row) (setf counter (+ 1 counter)) - (cons counter (nth ind row))) - table))) - (when (or deps (>= ind 0)) ;; remove non-plotting columns - (setf deps (delq ind deps)) - (setf table (mapcar (lambda (row) - (dotimes (col (length row)) - (unless (memq col deps) - (setf (nth col row) nil))) - (delq nil row)) - table))) +PARAMS specifies which columns of TABLE should be plotted as +independent and dependent variables. Return the ind-column, as +list of (cons 0-based-row-num . cell-value)." + (let* ((last-col (1- (length (car table)))) + (ind (1- (plist-get params :ind))) + (deps (delq ind (if (plist-member params :deps) + (mapcar #'1- (plist-get params :deps)) + (number-sequence 0 last-col)))) + (skip-deps (set-difference + (number-sequence 0 last-col) + deps)) + (row-vals ; the return value - indexed values from the ind column + (unless (< ind 0) + (let ((i -1)) (mapcar (lambda (row) + (incf i) + (cons i (nth ind row))) + table))))) + ;; remove non-plotting columns + (setq table (mapcar (lambda (row) + (org-remove-by-index row skip-deps)) + table)) ;; write table to gnuplot grid datafile format (with-temp-file data-file - (let ((num-rows (length table)) (num-cols (length (nth 0 table))) - (gnuplot-row (lambda (col row value) - (setf col (+ 1 col)) (setf row (+ 1 row)) - (format "%f %f %f\n%f %f %f\n" - col (- row 0.5) value ;; lower edge - col (+ row 0.5) value))) ;; upper edge - front-edge back-edge) - (dotimes (col num-cols) - (dotimes (row num-rows) - (setf back-edge - (concat back-edge - (funcall gnuplot-row (- col 1) row - (string-to-number (nth col (nth row table)))))) - (setf front-edge - (concat front-edge - (funcall gnuplot-row col row - (string-to-number (nth col (nth row table))))))) - ;; only insert once per row - (insert back-edge) (insert "\n") ;; back edge - (insert front-edge) (insert "\n") ;; front edge - (setf back-edge "") (setf front-edge "")))) + (let ((gnuplot-row (lambda (c r value) + (format "%f %f %s\n%f %f %s\n" + c (+ r 0.5) value + c (+ r 1.5) value)))) + (dotimes (c (length (car table))) + (let ((column (mapcar (lambda (row) (nth c row)) table)) + (r 0) + front-edge back-edge) + (dolist (cell column) + (setq back-edge (concat back-edge + (funcall gnuplot-row c r cell))) + (setq front-edge (concat front-edge + (funcall gnuplot-row (1+ c) r cell))) + (incf r)) + (insert (concat back-edge "\n" front-edge "\n")))))) row-vals)) (defun org-plot/gnuplot-script (data-file num-cols params &optional preface) -- 2.20.1 --------------10417FBC706EF47F73D90255--