Merge lp:~nik90/ubuntu-clock-app/migrate-stopwatch-utils-c++ into lp:ubuntu-clock-app

Proposed by Nekhelesh Ramananthan
Status: Merged
Approved by: Bartosz Kosiorek
Approved revision: 367
Merged at revision: 361
Proposed branch: lp:~nik90/ubuntu-clock-app/migrate-stopwatch-utils-c++
Merge into: lp:ubuntu-clock-app
Diff against target: 498 lines (+163/-113)
11 files modified
app/stopwatch/CMakeLists.txt (+0/-1)
app/stopwatch/LapListView.qml (+7/-6)
app/stopwatch/StopwatchFace.qml (+5/-4)
app/stopwatch/StopwatchPage.qml (+1/-1)
app/stopwatch/StopwatchUtils.qml (+0/-73)
backend/CMakeLists.txt (+13/-12)
backend/modules/Stopwatch/backend.cpp (+3/-1)
backend/modules/Stopwatch/formattime.cpp (+78/-0)
backend/modules/Stopwatch/formattime.h (+38/-0)
backend/modules/Stopwatch/qmldir (+2/-2)
tests/unit/tst_stopwatchUtils.qml (+16/-13)
To merge this branch: bzr merge lp:~nik90/ubuntu-clock-app/migrate-stopwatch-utils-c++
Reviewer Review Type Date Requested Status
Bartosz Kosiorek Approve
Ubuntu Phone Apps Jenkins Bot continuous-integration Approve
Review via email: mp+269051@code.launchpad.net

Commit message

Converted StopwatchUtils.qml functions to C++ functions to shave loading times and improve performance by a tiny bit.

Description of the change

This MP converts the StopwatchUtils.qml functions to C++ functions for the following reasons,

When I ran stopwatch using Qt Profiler, I noticed that StopwatchUtils{} takes around 60 ms to load, and each function call within takes an average of 200 microsecond. On converting this to c++, the load time reduces to 500 microseconds, so from 60ms->500microsecond! And each function call takes half the time (100 microsecond)

While the performance shaving are quite small, it raises the question as to why not do this change? I see no disadvantages. Nothing in the qml side needs to be changed, unit tests pass. Regression free!

To post a comment you must log in.
Revision history for this message
Ubuntu Phone Apps Jenkins Bot (ubuntu-phone-apps-jenkins-bot) wrote :
review: Approve (continuous-integration)
364. By Nekhelesh Ramananthan

Changed StopwatchUtils to just Utils

Revision history for this message
Ubuntu Phone Apps Jenkins Bot (ubuntu-phone-apps-jenkins-bot) wrote :
review: Approve (continuous-integration)
365. By Nekhelesh Ramananthan

Renamed Utils to FormatTime

366. By Nekhelesh Ramananthan

Fixed hours being truncated when they are greater than 99

367. By Nekhelesh Ramananthan

Fixed unit tests

Revision history for this message
Ubuntu Phone Apps Jenkins Bot (ubuntu-phone-apps-jenkins-bot) wrote :
review: Needs Fixing (continuous-integration)
Revision history for this message
Ubuntu Phone Apps Jenkins Bot (ubuntu-phone-apps-jenkins-bot) wrote :
review: Approve (continuous-integration)
Revision history for this message
Bartosz Kosiorek (gang65) wrote :

works perfectly for me, and everrything is fine according to checklist

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'app/stopwatch/CMakeLists.txt'
2--- app/stopwatch/CMakeLists.txt 2015-08-09 20:59:10 +0000
3+++ app/stopwatch/CMakeLists.txt 2015-08-26 22:51:08 +0000
4@@ -2,7 +2,6 @@
5 LapListView.qml
6 StopwatchFace.qml
7 StopwatchPage.qml
8- StopwatchUtils.qml
9 )
10
11 # make the files visible in the qtcreator tree
12
13=== modified file 'app/stopwatch/LapListView.qml'
14--- app/stopwatch/LapListView.qml 2015-08-20 19:59:21 +0000
15+++ app/stopwatch/LapListView.qml 2015-08-26 22:51:08 +0000
16@@ -19,14 +19,15 @@
17 import QtQuick 2.4
18 import QtQuick.Layouts 1.1
19 import Ubuntu.Components 1.2
20+import Stopwatch 1.0
21
22 ListView {
23 id: lapListView
24
25 clip: true
26
27- StopwatchUtils {
28- id: stopwatchUtils
29+ StopwatchFormatTime {
30+ id: stopwatchFormatTime
31 }
32
33 header: ListItem {
34@@ -108,11 +109,11 @@
35 Row {
36 anchors.horizontalCenter: parent.horizontalCenter
37 Label {
38- text: stopwatchUtils.lapTimeToString(model.laptime) + "."
39+ text: stopwatchFormatTime.lapTimeToString(model.laptime) + "."
40 }
41 Label {
42 fontSize: "x-small"
43- text: stopwatchUtils.millisToString(model.laptime)
44+ text: stopwatchFormatTime.millisToString(model.laptime)
45 anchors.bottom: parent.bottom
46 anchors.bottomMargin: units.dp(1)
47 }
48@@ -125,11 +126,11 @@
49 Row {
50 anchors.horizontalCenter: parent.horizontalCenter
51 Label {
52- text: stopwatchUtils.lapTimeToString(model.totaltime) + "."
53+ text: stopwatchFormatTime.lapTimeToString(model.totaltime) + "."
54 }
55 Label {
56 fontSize: "x-small"
57- text: stopwatchUtils.millisToString(model.totaltime)
58+ text: stopwatchFormatTime.millisToString(model.totaltime)
59 anchors.bottom: parent.bottom
60 anchors.bottomMargin: units.dp(1)
61 }
62
63=== modified file 'app/stopwatch/StopwatchFace.qml'
64--- app/stopwatch/StopwatchFace.qml 2015-08-17 20:27:12 +0000
65+++ app/stopwatch/StopwatchFace.qml 2015-08-26 22:51:08 +0000
66@@ -17,6 +17,7 @@
67 */
68
69 import QtQuick 2.4
70+import Stopwatch 1.0
71 import Ubuntu.Components 1.2
72 import "../components"
73
74@@ -29,8 +30,8 @@
75 isOuter: true
76 width: units.gu(32)
77
78- StopwatchUtils {
79- id: stopwatchUtils
80+ StopwatchFormatTime {
81+ id: stopwatchFormatTime
82 }
83
84 ClockCircle {
85@@ -46,13 +47,13 @@
86 anchors.centerIn: parent
87
88 Label {
89- text: stopwatchUtils.millisToTimeString(milliseconds, false, true)
90+ text: stopwatchFormatTime.millisToTimeString(milliseconds, true)
91 font.pixelSize: units.dp(34)
92 color: UbuntuColors.midAubergine
93 }
94
95 Label {
96- text: stopwatchUtils.millisToString(milliseconds)
97+ text: stopwatchFormatTime.millisToString(milliseconds)
98 font.pixelSize: units.dp(18)
99 color: UbuntuColors.midAubergine
100 anchors.horizontalCenter: parent.horizontalCenter
101
102=== modified file 'app/stopwatch/StopwatchPage.qml'
103--- app/stopwatch/StopwatchPage.qml 2015-08-18 13:13:32 +0000
104+++ app/stopwatch/StopwatchPage.qml 2015-08-26 22:51:08 +0000
105@@ -17,8 +17,8 @@
106 */
107
108 import QtQuick 2.4
109+import Stopwatch 1.0
110 import Ubuntu.Components 1.2
111-import Stopwatch.LapHistory 1.0
112
113 Item {
114 id: _stopwatchPage
115
116=== removed file 'app/stopwatch/StopwatchUtils.qml'
117--- app/stopwatch/StopwatchUtils.qml 2015-08-17 20:27:12 +0000
118+++ app/stopwatch/StopwatchUtils.qml 1970-01-01 00:00:00 +0000
119@@ -1,73 +0,0 @@
120-/*
121- * Copyright (C) 2015 Canonical Ltd
122- *
123- * This file is part of Ubuntu Clock App
124- *
125- * Ubuntu Clock App is free software: you can redistribute it and/or modify
126- * it under the terms of the GNU General Public License version 3 as
127- * published by the Free Software Foundation.
128- *
129- * Ubuntu Clock App is distributed in the hope that it will be useful,
130- * but WITHOUT ANY WARRANTY; without even the implied warranty of
131- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
132- * GNU General Public License for more details.
133- *
134- * You should have received a copy of the GNU General Public License
135- * along with this program. If not, see <http://www.gnu.org/licenses/>.
136- */
137-
138-import QtQuick 2.4
139-import Ubuntu.Components 1.2
140-
141-/*
142- Qt Object containing a collection of useful stopwatch functions
143-*/
144-QtObject {
145- id: stopwatchUtils
146-
147- // Function to return only the milliseconds
148- function millisToString(millis) {
149- return addZeroPrefix(millis % 1000, 3)
150- }
151-
152- // Function to break down time (milliseconds) to hours, minutes and seconds
153- function millisToTimeString(millis, showMilliseconds, showHours) {
154- var hours, minutes, seconds
155-
156- // Break down total time (milliseconds) to hours, minutes and seconds
157- seconds = Math.floor(millis / 1000) % 60;
158- minutes = Math.floor(millis / 1000 / 60) % 60
159- hours = Math.floor(millis / 1000 / 60 / 60)
160-
161- // Build the time string
162- var timeString = ""
163-
164- if (showHours) {
165- timeString += addZeroPrefix(hours, 2) + ":"
166- }
167-
168- timeString += addZeroPrefix(minutes, 2) + ":"
169- timeString += addZeroPrefix(seconds, 2)
170-
171- if (showMilliseconds) {
172- timeString += "." + millisToString(millis)
173- }
174-
175- return timeString
176- }
177-
178- // Function to add zero prefix if necessary.
179- function addZeroPrefix (str, totalLength) {
180- return ("00000" + str).slice(-totalLength)
181- }
182-
183- // Function to return lap time as a string
184- function lapTimeToString(millis) {
185- var hours = hours = Math.floor(millis / 1000 / 60 / 60)
186- if (hours > 0) {
187- return millisToTimeString(millis, false, true)
188- } else {
189- return millisToTimeString(millis, false, false)
190- }
191- }
192-}
193
194=== modified file 'backend/CMakeLists.txt'
195--- backend/CMakeLists.txt 2015-08-24 15:08:21 +0000
196+++ backend/CMakeLists.txt 2015-08-26 22:51:08 +0000
197@@ -41,9 +41,10 @@
198 )
199
200 set(
201- stopwatchlaphistory_SRCS
202- modules/Stopwatch/LapHistory/backend.cpp
203- modules/Stopwatch/LapHistory/history.cpp
204+ stopwatch_SRCS
205+ modules/Stopwatch/backend.cpp
206+ modules/Stopwatch/history.cpp
207+ modules/Stopwatch/formattime.cpp
208 )
209
210 add_library(timezone MODULE
211@@ -66,8 +67,8 @@
212 ${clockutility_SRCS}
213 )
214
215-add_library(stopwatchlaphistory MODULE
216- ${stopwatchlaphistory_SRCS}
217+add_library(stopwatch MODULE
218+ ${stopwatch_SRCS}
219 )
220
221 set_target_properties(timezone PROPERTIES
222@@ -90,8 +91,8 @@
223 LIBRARY_OUTPUT_DIRECTORY Clock/Utility
224 )
225
226-set_target_properties(stopwatchlaphistory PROPERTIES
227- LIBRARY_OUTPUT_DIRECTORY Stopwatch/LapHistory
228+set_target_properties(stopwatch PROPERTIES
229+ LIBRARY_OUTPUT_DIRECTORY Stopwatch/
230 )
231
232 qt5_use_modules(datetime Gui Qml Quick)
233@@ -99,7 +100,7 @@
234 qt5_use_modules(alarmsettings Gui Qml Quick DBus)
235 qt5_use_modules(geolocation Gui Qml Quick)
236 qt5_use_modules(clockutility Qml)
237-qt5_use_modules(stopwatchlaphistory Qml)
238+qt5_use_modules(stopwatch Qml)
239
240 # Copy qmldir file to build dir for running in QtCreator
241 add_custom_target(timezone-qmldir ALL
242@@ -127,8 +128,8 @@
243 DEPENDS ${QMLFILES}
244 )
245
246-add_custom_target(stopwatchlaphistory-qmldir ALL
247- COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/modules/Stopwatch/LapHistory/qmldir ${CMAKE_CURRENT_BINARY_DIR}/Stopwatch/LapHistory
248+add_custom_target(stopwatch-qmldir ALL
249+ COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/modules/Stopwatch/qmldir ${CMAKE_CURRENT_BINARY_DIR}/Stopwatch
250 DEPENDS ${QMLFILES}
251 )
252
253@@ -148,6 +149,6 @@
254 install(TARGETS clockutility DESTINATION ${MODULE_PATH}/Clock/Utility/)
255 install(FILES modules/Clock/Utility/qmldir DESTINATION ${MODULE_PATH}/Clock/Utility/)
256
257-install(TARGETS stopwatchlaphistory DESTINATION ${MODULE_PATH}/Stopwatch/LapHistory/)
258-install(FILES modules/Stopwatch/LapHistory/qmldir DESTINATION ${MODULE_PATH}/Stopwatch/LapHistory/)
259+install(TARGETS stopwatch DESTINATION ${MODULE_PATH}/Stopwatch/)
260+install(FILES modules/Stopwatch/qmldir DESTINATION ${MODULE_PATH}/Stopwatch/)
261
262
263=== removed directory 'backend/modules/Stopwatch/LapHistory'
264=== renamed file 'backend/modules/Stopwatch/LapHistory/backend.cpp' => 'backend/modules/Stopwatch/backend.cpp'
265--- backend/modules/Stopwatch/LapHistory/backend.cpp 2015-08-18 05:03:59 +0000
266+++ backend/modules/Stopwatch/backend.cpp 2015-08-26 22:51:08 +0000
267@@ -20,12 +20,14 @@
268 #include <QtQml/QQmlContext>
269 #include "backend.h"
270 #include "history.h"
271+#include "formattime.h"
272
273 void BackendPlugin::registerTypes(const char *uri)
274 {
275- Q_ASSERT(uri == QLatin1String("Stopwatch.LapHistory"));
276+ Q_ASSERT(uri == QLatin1String("Stopwatch"));
277
278 qmlRegisterType<LapHistory>(uri, 1, 0, "LapHistory");
279+ qmlRegisterType<FormatTime>(uri, 1, 0, "StopwatchFormatTime");
280 }
281
282 void BackendPlugin::initializeEngine(QQmlEngine *engine, const char *uri)
283
284=== renamed file 'backend/modules/Stopwatch/LapHistory/backend.h' => 'backend/modules/Stopwatch/backend.h'
285=== added file 'backend/modules/Stopwatch/formattime.cpp'
286--- backend/modules/Stopwatch/formattime.cpp 1970-01-01 00:00:00 +0000
287+++ backend/modules/Stopwatch/formattime.cpp 2015-08-26 22:51:08 +0000
288@@ -0,0 +1,78 @@
289+/*
290+ * Copyright (C) 2015 Canonical Ltd
291+ *
292+ * This file is part of Ubuntu Clock App
293+ *
294+ * Ubuntu Clock App is free software: you can redistribute it and/or modify
295+ * it under the terms of the GNU General Public License version 3 as
296+ * published by the Free Software Foundation.
297+ *
298+ * Ubuntu Clock App is distributed in the hope that it will be useful,
299+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
300+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
301+ * GNU General Public License for more details.
302+ *
303+ * You should have received a copy of the GNU General Public License
304+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
305+ */
306+
307+#include "formattime.h"
308+
309+#include <QtMath>
310+
311+FormatTime::FormatTime(QObject *parent):
312+ QObject(parent)
313+{
314+}
315+
316+QString FormatTime::millisToString(int millis) const
317+{
318+ return addZeroPrefix(QString::number(millis), 3);
319+}
320+
321+QString FormatTime::millisToTimeString(int millis, bool showHours) const
322+{
323+ int hours, minutes, seconds;
324+
325+ seconds = qFloor(millis / 1000) % 60;
326+ minutes = qFloor(millis / 1000 / 60) % 60;
327+ hours = qFloor(millis / 1000 / 60 / 60);
328+
329+ QString timeString("");
330+
331+ if (showHours)
332+ {
333+ if (hours < 10)
334+ {
335+ timeString += addZeroPrefix(QString::number(hours), 2) + ":";
336+ }
337+
338+ else {
339+ timeString += QString::number(hours) + ":";
340+ }
341+ }
342+
343+ timeString += addZeroPrefix(QString::number(minutes), 2) + ":";
344+ timeString += addZeroPrefix(QString::number(seconds), 2);
345+
346+ return timeString;
347+}
348+
349+QString FormatTime::addZeroPrefix(QString str, int totalLength) const
350+{
351+ return QString("00000" + str).remove(0, 5 + str.length() - totalLength);
352+}
353+
354+QString FormatTime::lapTimeToString(int millis) const
355+{
356+ int hours = qFloor(millis / 1000 / 60 / 60);
357+
358+ if (hours > 0)
359+ {
360+ return millisToTimeString(millis, true);
361+ }
362+
363+ else {
364+ return millisToTimeString(millis, false);
365+ }
366+}
367
368=== added file 'backend/modules/Stopwatch/formattime.h'
369--- backend/modules/Stopwatch/formattime.h 1970-01-01 00:00:00 +0000
370+++ backend/modules/Stopwatch/formattime.h 2015-08-26 22:51:08 +0000
371@@ -0,0 +1,38 @@
372+/*
373+ * Copyright (C) 2015 Canonical Ltd
374+ *
375+ * This file is part of Ubuntu Clock App
376+ *
377+ * Ubuntu Clock App is free software: you can redistribute it and/or modify
378+ * it under the terms of the GNU General Public License version 3 as
379+ * published by the Free Software Foundation.
380+ *
381+ * Ubuntu Clock App is distributed in the hope that it will be useful,
382+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
383+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
384+ * GNU General Public License for more details.
385+ *
386+ * You should have received a copy of the GNU General Public License
387+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
388+ */
389+
390+#ifndef FORMATTIME_H
391+#define FORMATTIME_H
392+
393+#include <QObject>
394+
395+class FormatTime: public QObject
396+{
397+ Q_OBJECT
398+
399+public:
400+ FormatTime(QObject *parent=0);
401+
402+public slots:
403+ QString millisToString(int millis) const;
404+ QString millisToTimeString(int millis, bool showHours) const;
405+ QString addZeroPrefix(QString str, int totalLength) const;
406+ QString lapTimeToString(int millis) const;
407+};
408+
409+#endif
410
411=== renamed file 'backend/modules/Stopwatch/LapHistory/history.cpp' => 'backend/modules/Stopwatch/history.cpp'
412=== renamed file 'backend/modules/Stopwatch/LapHistory/history.h' => 'backend/modules/Stopwatch/history.h'
413=== renamed file 'backend/modules/Stopwatch/LapHistory/qmldir' => 'backend/modules/Stopwatch/qmldir'
414--- backend/modules/Stopwatch/LapHistory/qmldir 2015-08-18 05:03:59 +0000
415+++ backend/modules/Stopwatch/qmldir 2015-08-26 22:51:08 +0000
416@@ -1,2 +1,2 @@
417-module Stopwatch.LapHistory
418-plugin stopwatchlaphistory
419+module Stopwatch
420+plugin stopwatch
421
422=== modified file 'tests/unit/tst_stopwatchUtils.qml'
423--- tests/unit/tst_stopwatchUtils.qml 2015-08-17 20:27:12 +0000
424+++ tests/unit/tst_stopwatchUtils.qml 2015-08-26 22:51:08 +0000
425@@ -18,15 +18,16 @@
426
427 import QtQuick 2.4
428 import QtTest 1.0
429+import Stopwatch 1.0
430 import Ubuntu.Components 1.2
431 import "../../app/stopwatch"
432
433 TestCase {
434- id: stopwatchUtilsTest
435- name: "StopwatchUtilsLibrary"
436+ id: stopwatchFormatTimeTest
437+ name: "StopwatchFormatTimeLibrary"
438
439- StopwatchUtils {
440- id: stopwatchUtils
441+ StopwatchFormatTime {
442+ id: stopwatchFormatTime
443 }
444
445 /*
446@@ -35,9 +36,9 @@
447 */
448 function test_returnMillisecond() {
449 var result
450- result = stopwatchUtils.millisToString(400)
451+ result = stopwatchFormatTime.millisToString(400)
452 compare(result, "400", "Milliseconds not properly converted to the format required")
453- result = stopwatchUtils.millisToString(4)
454+ result = stopwatchFormatTime.millisToString(4)
455 compare(result, "004", "Milliseconds not properly converted to the format required")
456 }
457
458@@ -46,9 +47,11 @@
459 correctly.
460 */
461 function test_convertTimeInMillisecondsToString() {
462- var timeInMilliseconds = 1123000
463- var result = stopwatchUtils.millisToTimeString(timeInMilliseconds, false, true)
464+ var timeInMilliseconds = 1123000, result
465+ result = stopwatchFormatTime.millisToTimeString(timeInMilliseconds, true)
466 compare(result, "00:18:43", "Time not properly converted from milliseconds to hh:mm:ss")
467+ result = stopwatchFormatTime.millisToTimeString(timeInMilliseconds, false)
468+ compare(result, "18:43", "Time not properly converted from milliseconds to mm:ss")
469 }
470
471 /*
472@@ -57,11 +60,11 @@
473 */
474 function test_zeroPrefixAddedCorrectly() {
475 var str = "32", result
476- result = stopwatchUtils.addZeroPrefix(str, 2)
477+ result = stopwatchFormatTime.addZeroPrefix(str, 2)
478 compare(result, "32", "Zero prefix not added correctly")
479- result = stopwatchUtils.addZeroPrefix(str, 3)
480+ result = stopwatchFormatTime.addZeroPrefix(str, 3)
481 compare(result, "032", "Zero prefix not added correctly")
482- result = stopwatchUtils.addZeroPrefix(str, 4)
483+ result = stopwatchFormatTime.addZeroPrefix(str, 4)
484 compare(result, "0032", "Zero prefix not added correctly")
485 }
486
487@@ -71,9 +74,9 @@
488 */
489 function test_lapTimeIncludesHoursCorrectly() {
490 var result
491- result = stopwatchUtils.lapTimeToString(1123000)
492+ result = stopwatchFormatTime.lapTimeToString(1123000)
493 compare(result, "18:43", "Lap time shows hours despite it not being greater than 0")
494- result = stopwatchUtils.lapTimeToString(8323000)
495+ result = stopwatchFormatTime.lapTimeToString(8323000)
496 compare(result, "02:18:43", "Lap time not showing hours despite it being greater than 0")
497 }
498 }

Subscribers

People subscribed via source and target branches