IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

R Discussion :

Optimisation temps de traitement


Sujet :

R

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre averti
    Homme Profil pro
    Epitech
    Inscrit en
    Mai 2019
    Messages
    33
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 28
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Epitech

    Informations forums :
    Inscription : Mai 2019
    Messages : 33
    Par défaut Optimisation temps de traitement
    Bonjour,

    Je travaille actuellement sur de la mise en forme sur Excel à l'aide de R, je vous joins mon code pour avoir un exemple des données sur lesquelles je travailles (en l'occurrence cela correspond au data frame "df"). J'ai laissé un commentaire pour soit changer le chemin du fichier pour vous donner un aperçu du résultat ou alors il me semble qu'il y a une fonction qui permet de visualiser le résultat sans pour autant créer un fichier comme je l'ai fait.
    Mon problème étant que sur un data frame plus grand le temps de traitement est assez long, environ 4min pour un data frame de 359 lignes, c'est pourquoi j'aimerais déjà comprendre pourquoi est-ce si long (le fait d'utiliser une boucle je pense, ou alors le nombre d'appel fonction qui sont dans la boucle ?) ? Et comment puis-je améliorer mon code pour que le temps de traitement soit plus court ?

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
     
    library(readxl)
    library(openxlsx)
     
    df <- data.frame("Nom" = c("ABC", "DEF"),
                     "R" = c("R1", "R2"),
                     "Nom1" = c("AZER", "AZER"),
                     "R1" = c("A1", "B1"),
                     "C1" = c("C1", "C1"),
                     "D1" = c("D1", "D1"),
                     "P1" = c(10, 20),
                     "Nom2" = c("AZERTY", "AZERTY"),
                     "R2" = c("A2", "B2"),
                     "C2" = c("C2", "C2"),
                     "D2" = c("D2", "D2"),
                     "P2" = c(10, 20),
                     "Nom3" = c("AZERTYUI", "AZERTYUI"),
                     "R3" = c("A3", "B3"),
                     "C3" = c("C3", "C3"),
                     "D3" = c("D3", "D3"),
                     "P3" = c(10, 20)
                     )
    wb <- createWorkbook()
    sheet <- "Sheet1"
    addWorksheet(wb = wb, sheetName = sheet)
     
    mergeCells(wb = wb, sheet = sheet, cols = 1:15, rows = 1)
    mergeCells(wb = wb, sheet = sheet, cols = 1:15, rows = 2)
    writeData(wb = wb, sheet = sheet, x = "Example", startCol = 1, startRow = 1)
    writeData(wb = wb, sheet = sheet, x = "Example", startCol = 1, startRow = 2)
     
    a <- createStyle(halign = "center", valign = "center", border = "TopRightBottomLeft", borderStyle = "medium", textDecoration = "bold", fgFill = "#00AAFF")
    f <- createStyle(halign = "center", valign = "center", textDecoration = "bold", border = "TopRightBottomLeft", borderStyle = "medium", fgFill = "#D3D3D3")
    r <- createStyle(halign = "center", valign = "center", border = "TopRightBottomLeft", borderStyle = "medium")
    nom <- createStyle(halign = "center", valign = "center", textDecoration = "bold", fontSize = 12)
    pro <- createStyle(halign = "left", fontSize = 9)
    blue <- createStyle(fgFill = "#0088FF")
    Hpro <- createStyle(halign = "left", textDecoration = "bold")
    condi <- createStyle(halign = "center", valign = "center", fontSize = 9, border = "TopRightBottomLeft", borderStyle = "medium", fgFill = "#D3D3D3")
    white <- createStyle(fgFill = "#FFFFFF")
    border <- createStyle(border = "TopRightBottomLeft", borderStyle = "medium")
    yellow <- createStyle(fgFill = "#F1C40F", halign = "center", valign = "center", textDecoration = "bold", fontSize = 12)
    title <- createStyle(fontSize = 18, textDecoration = "bold", halign = "center", valign = "center", border = "TopRightBottomLeft", borderStyle = "medium", fgFill = "#FFFFFF")
     
    addStyle(wb = wb, sheet = sheet, style = title, rows = 1, cols = 1:15, gridExpand = TRUE)
    addStyle(wb = wb, sheet = sheet, style = yellow, rows = 2, cols = 1:15, gridExpand = TRUE)
    setRowHeights(wb = wb, sheet = sheet, rows = 1, heights = 100)
    setRowHeights(wb = wb, sheet = sheet, rows = 2, heights = 20)
     
    x <- 1
    end <- nrow(df)
    while (x <= end) {
      mergeCells(wb = wb, sheet = sheet, cols = 1, rows = (x + 2 + ((x - 1) * 4)):(x + 5 + ((x - 1) * 4))) # merge photos
     
      addStyle(wb = wb, sheet = sheet, style = white, rows = (x + 2 + ((x - 1) * 4)):(x + 5 + ((x - 1) * 4)), cols = 1:15, gridExpand = TRUE)
      addStyle(wb = wb, sheet = sheet, style = border, rows = (x + 2 + ((x - 1) * 4)):(x + 5 + ((x - 1) * 4)), cols = 1, gridExpand = TRUE)
     
      mergeCells(wb = wb, sheet = sheet, cols = 3:4, rows = (x + 2 + ((x - 1) * 4)):(x + 5 + ((x - 1) * 4))) # merge D
     
      writeData(wb = wb, sheet = sheet, x = df[x, "Nom"], startCol = 3, startRow = (x + 2 + ((x - 1) * 4)), colNames = FALSE)
     
      addStyle(wb = wb, sheet = sheet, style = nom, cols = 3, rows = (x + 2 + ((x - 1) * 4)), stack = TRUE)
     
      mergeCells(wb = wb, sheet = sheet, cols = c("E", "F", "G"), rows = (x + 2 + ((x - 1) * 4)))
      mergeCells(wb = wb, sheet = sheet, cols = c("E", "F", "G"), rows = (x + 2 + ((x - 1) * 4)) + 1)
      mergeCells(wb = wb, sheet = sheet, cols = c("I", "J", "K"), rows = (x + 2 + ((x - 1) * 4)))
      mergeCells(wb = wb, sheet = sheet, cols = c("I", "J", "K"), rows = (x + 2 + ((x - 1) * 4)) + 1)
      mergeCells(wb = wb, sheet = sheet, cols = c("M", "N", "O"), rows = (x + 2 + ((x - 1) * 4)))
      mergeCells(wb = wb, sheet = sheet, cols = c("M", "N", "O"), rows = (x + 2 + ((x - 1) * 4)) + 1)
     
      writeData(wb = wb, sheet = sheet, x = data.frame("a" = c(as.character(df[x, "Nom1"]), as.character(df[x, "D1"])),
                                                       "b" = "", "c" = "", "d" = "",
                                                       "e" = c(as.character(df[x, "Nom2"]), as.character(df[x, "D2"])),
                                                       "f" = "", "g" = "", "h" = "",
                                                       "i" = c(as.character(df[x, "Nom3"]), as.character(df[x, "D3"]))),
                startCol = "E", startRow = (x + 2 + ((x - 1) * 4)), colNames = FALSE)
      addStyle(wb = wb, sheet = sheet, style = condi, cols = c("E", "F", "G", "I", "J", "K", "M", "N", "O"), rows = (x + 2 + ((x - 1) * 4)) + 1, gridExpand = TRUE, stack = TRUE)
      addStyle(wb = wb, sheet = sheet, style = f, cols = c("E", "F", "G", "I", "J", "K", "M", "N", "O"), rows = (x + 2 + ((x - 1) * 4)), gridExpand = TRUE, stack = TRUE)
     
      writeData(wb = wb, sheet = sheet, x = data.frame("a" = c("R", as.character(df[x, "R"]))), startCol = "B", startRow = (x + 2 + ((x - 1) * 4)) + 2, colNames = FALSE)
      addStyle(wb = wb, sheet = sheet, style = Hpro, cols = "B", rows = (x + 2 + ((x - 1) * 4)) + 2, gridExpand = TRUE, stack = TRUE)
      addStyle(wb = wb, sheet = sheet, style = pro, cols = "B", rows = (x + 2 + ((x - 1) * 4)) + 3, gridExpand = TRUE, stack = TRUE)
     
      writeData(wb = wb, sheet = sheet, x = data.frame("a" = c("R", as.character(df[x, "R1"])), "b" = c("P", as.double(df[x, "P1"])), "c" = c("C", as.character(df[x, "C1"])), "d" = "",
                                                       "e" = c("R", as.character(df[x, "R2"])), "f" = c("P", as.double(df[x, "P2"])), "g" = c("C", as.character(df[x, "C2"])), "h" = "",
                                                       "i" = c("R", as.character(df[x, "R3"])), "j" = c("P", as.double(df[x, "P3"])), "k" = c("C", as.character(df[x, "C3"]))),
                startCol = "E", startRow = (x + 2 + ((x - 1) * 4)) + 2, colNames = FALSE)
     
      addStyle(wb = wb, sheet = sheet, style = a, rows = (x + 5 + ((x - 1) * 4)) - 1, cols = c("E", "F", "G", "I", "J", "K", "M", "N", "O"), gridExpand = TRUE, stack = TRUE)
      addStyle(wb = wb, sheet = sheet, style = r, rows = (x + 5 + ((x - 1) * 4)), cols = c("E", "F", "G", "I", "J", "K", "M", "N", "O"), gridExpand = TRUE, stack = TRUE)
      addStyle(wb = wb, sheet = sheet, style = blue, rows = (x + 5 + ((x - 1) * 4)) + 1, cols = 1:15, gridExpand = TRUE, stack = TRUE)
      setRowHeights(wb = wb, sheet = sheet, rows = (x + 5 + ((x - 1) * 4)) + 1, heights = 5)
      #print(x)
      x = x + 1
    }
     
    setColWidths(wb = wb, sheet = sheet, cols = c("B", "C"), widths = "auto")
    setColWidths(wb = wb, sheet = sheet, cols = c("E", "F", "G", "I", "J", "K", "M", "N", "O"), widths = 10)
    setColWidths(wb = wb, sheet = sheet, cols = c("H", "L"), widths = 1)
     
    saveWorkbook(wb = wb, file = "Example.xlsx", overwrite = TRUE) # Mettre le chemin ou l'on veut que le fichier soit stocker
    Cordialement,

    Arkning

  2. #2
    Membre Expert
    Inscrit en
    Novembre 2009
    Messages
    707
    Détails du profil
    Informations forums :
    Inscription : Novembre 2009
    Messages : 707
    Par défaut Optimisation temps de traitement
    Bonjour,

    Vous gagnerez un peu en remplaçant la boucle par la fonction sapply().

    • Création d'une fonction mef() avec le contenu de votre boucle

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    mef <- function(x) {
      mergeCells(wb = wb, sheet = sheet, cols = 1, rows = (x + 2 + ((x - 1) * 4)):(x + 5 + ((x - 1) * 4))) # merge photos
     
      addStyle(wb = wb, sheet = sheet, style = white, rows = (x + 2 + ((x - 1) * 4)):(x + 5 + ((x - 1) * 4)), cols = 1:15, gridExpand = TRUE)
      addStyle(wb = wb, sheet = sheet, style = border, rows = (x + 2 + ((x - 1) * 4)):(x + 5 + ((x - 1) * 4)), cols = 1, gridExpand = TRUE)
     
      mergeCells(wb = wb, sheet = sheet, cols = 3:4, rows = (x + 2 + ((x - 1) * 4)):(x + 5 + ((x - 1) * 4))) # merge D
     
      writeData(wb = wb, sheet = sheet, x = df[x, "Nom"], startCol = 3, startRow = (x + 2 + ((x - 1) * 4)), colNames = FALSE)
     
      addStyle(wb = wb, sheet = sheet, style = nom, cols = 3, rows = (x + 2 + ((x - 1) * 4)), stack = TRUE)
     
      mergeCells(wb = wb, sheet = sheet, cols = c("E", "F", "G"), rows = (x + 2 + ((x - 1) * 4)))
      mergeCells(wb = wb, sheet = sheet, cols = c("E", "F", "G"), rows = (x + 2 + ((x - 1) * 4)) + 1)
      mergeCells(wb = wb, sheet = sheet, cols = c("I", "J", "K"), rows = (x + 2 + ((x - 1) * 4)))
      mergeCells(wb = wb, sheet = sheet, cols = c("I", "J", "K"), rows = (x + 2 + ((x - 1) * 4)) + 1)
      mergeCells(wb = wb, sheet = sheet, cols = c("M", "N", "O"), rows = (x + 2 + ((x - 1) * 4)))
      mergeCells(wb = wb, sheet = sheet, cols = c("M", "N", "O"), rows = (x + 2 + ((x - 1) * 4)) + 1)
     
      writeData(wb = wb, sheet = sheet, x = data.frame("a" = c(as.character(df[x, "Nom1"]), as.character(df[x, "D1"])),
                                                       "b" = "", "c" = "", "d" = "",
                                                       "e" = c(as.character(df[x, "Nom2"]), as.character(df[x, "D2"])),
                                                       "f" = "", "g" = "", "h" = "",
                                                       "i" = c(as.character(df[x, "Nom3"]), as.character(df[x, "D3"]))),
                startCol = "E", startRow = (x + 2 + ((x - 1) * 4)), colNames = FALSE)
      addStyle(wb = wb, sheet = sheet, style = condi, cols = c("E", "F", "G", "I", "J", "K", "M", "N", "O"), rows = (x + 2 + ((x - 1) * 4)) + 1, gridExpand = TRUE, stack = TRUE)
      addStyle(wb = wb, sheet = sheet, style = f, cols = c("E", "F", "G", "I", "J", "K", "M", "N", "O"), rows = (x + 2 + ((x - 1) * 4)), gridExpand = TRUE, stack = TRUE)
     
      writeData(wb = wb, sheet = sheet, x = data.frame("a" = c("R", as.character(df[x, "R"]))), startCol = "B", startRow = (x + 2 + ((x - 1) * 4)) + 2, colNames = FALSE)
      addStyle(wb = wb, sheet = sheet, style = Hpro, cols = "B", rows = (x + 2 + ((x - 1) * 4)) + 2, gridExpand = TRUE, stack = TRUE)
      addStyle(wb = wb, sheet = sheet, style = pro, cols = "B", rows = (x + 2 + ((x - 1) * 4)) + 3, gridExpand = TRUE, stack = TRUE)
     
      writeData(wb = wb, sheet = sheet, x = data.frame("a" = c("R", as.character(df[x, "R1"])), "b" = c("P", as.double(df[x, "P1"])), "c" = c("C", as.character(df[x, "C1"])), "d" = "",
                                                       "e" = c("R", as.character(df[x, "R2"])), "f" = c("P", as.double(df[x, "P2"])), "g" = c("C", as.character(df[x, "C2"])), "h" = "",
                                                       "i" = c("R", as.character(df[x, "R3"])), "j" = c("P", as.double(df[x, "P3"])), "k" = c("C", as.character(df[x, "C3"]))),
                startCol = "E", startRow = (x + 2 + ((x - 1) * 4)) + 2, colNames = FALSE)
     
      addStyle(wb = wb, sheet = sheet, style = a, rows = (x + 5 + ((x - 1) * 4)) - 1, cols = c("E", "F", "G", "I", "J", "K", "M", "N", "O"), gridExpand = TRUE, stack = TRUE)
      addStyle(wb = wb, sheet = sheet, style = r, rows = (x + 5 + ((x - 1) * 4)), cols = c("E", "F", "G", "I", "J", "K", "M", "N", "O"), gridExpand = TRUE, stack = TRUE)
      addStyle(wb = wb, sheet = sheet, style = blue, rows = (x + 5 + ((x - 1) * 4)) + 1, cols = 1:15, gridExpand = TRUE, stack = TRUE)
      setRowHeights(wb = wb, sheet = sheet, rows = (x + 5 + ((x - 1) * 4)) + 1, heights = 5)
    }
    • Appel de la fonction mef() dans une boucle

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    > T1<-Sys.time()
    > x <- 1
    > end <- nrow(df)
    > while (x <= end) {
    +   mef(x)
    +   x <- x+1
    + }
    > T2<-Sys.time()
    > difftime(T2,T1)
    Time difference of 0.3432 secs
    • Appel de la fonction mef() dans la fonction sapply()
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    > T1<-Sys.time()
    > end <- nrow(df)
    > sapply(1:end, mef)
    [[1]]
        1     2     7 
    "100"  "20"   "5" 
     
    [[2]]
        1     2     7    12 
    "100"  "20"   "5"   "5" 
     
    > T2<-Sys.time()
    > difftime(T2,T1)
    Time difference of 0.2808011 secs
    Les fonctions de la famille apply() restent une sorte de boucle. Il est préférable de faire les opérations en parallèle lorsque c'est possible.

    Cette discussion vous intéressera peut-être.

    Cordialement,

  3. #3
    Membre averti
    Homme Profil pro
    Epitech
    Inscrit en
    Mai 2019
    Messages
    33
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 28
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Epitech

    Informations forums :
    Inscription : Mai 2019
    Messages : 33
    Par défaut
    Bonjour,

    Merci de votre retour cela m'a fait gagné du temps, dorénavant j'utiliserais sapply() plutôt que des while(), une idée m'est venu pour gagner du temps, lors de mes appels de fonction addStyle(), la plus part du temps je passe cette formule comme donnée : (x + 2 + ((x - 1) * 4)):(x + 5 + ((x - 1) * 4))
    Je voulais donc savoir s'il était possible de lui donner un vecteur qui contient tous les couples de données par rapport à cette formule petit exemple :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
     
    x <- 1
    end <- 3
     
    vecteur_1 <- (3, 4, 5, 6) # pour x = 1
    vecteur_2 <- (8, 9, 10, 11) # pour x = 2
    vecteur_3 <- (13, 14, 15, 16) # pour x = 3
    vecteur_final <- c(c(vecteur_1, vecteur_2), vecteur_3)
    Comment puis-je obtenir un vecteur qui correspond à l'exemple du dessus ?

    Cordialement,

    Arkning

  4. #4
    Membre Expert
    Inscrit en
    Novembre 2009
    Messages
    707
    Détails du profil
    Informations forums :
    Inscription : Novembre 2009
    Messages : 707
    Par défaut Optimisation temps de traitement
    Bonjour,

    Vous pouvez définir les variables xmin et xmax pour éviter les calculs multiples :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    xmin <- x + 2 + ((x - 1) * 4)
    xmax <- xmin + 3
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    mep <- function(x) {
      xmin <- x + 2 + ((x - 1) * 4)
      xmax <- xmin + 3
     
      mergeCells(wb = wb, sheet = sheet, cols = 1, rows = xmin:xmax) # merge photos
     
      addStyle(wb = wb, sheet = sheet, style = white, rows = xmin:xmax, cols = 1:15, gridExpand = TRUE)
      addStyle(wb = wb, sheet = sheet, style = border, rows = xmin:xmax, cols = 1, gridExpand = TRUE)
     
      mergeCells(wb = wb, sheet = sheet, cols = 3:4, rows = xmin:xmax) # merge D
     
      writeData(wb = wb, sheet = sheet, x = df[x, "Nom"], startCol = 3, startRow = xmin, colNames = FALSE)
     
      addStyle(wb = wb, sheet = sheet, style = nom, cols = 3, rows = xmin, stack = TRUE)
     
      mergeCells(wb = wb, sheet = sheet, cols = c("E", "F", "G"), rows = xmin)
      mergeCells(wb = wb, sheet = sheet, cols = c("E", "F", "G"), rows = xmin + 1)
      mergeCells(wb = wb, sheet = sheet, cols = c("I", "J", "K"), rows = xmin)
      mergeCells(wb = wb, sheet = sheet, cols = c("I", "J", "K"), rows = xmin + 1)
      mergeCells(wb = wb, sheet = sheet, cols = c("M", "N", "O"), rows = xmin)
      mergeCells(wb = wb, sheet = sheet, cols = c("M", "N", "O"), rows = xmin + 1)
     
      writeData(wb = wb, sheet = sheet, x = data.frame("a" = c(as.character(df[x, "Nom1"]), as.character(df[x, "D1"])),
                                                       "b" = "", "c" = "", "d" = "",
                                                       "e" = c(as.character(df[x, "Nom2"]), as.character(df[x, "D2"])),
                                                       "f" = "", "g" = "", "h" = "",
                                                       "i" = c(as.character(df[x, "Nom3"]), as.character(df[x, "D3"]))),
                startCol = "E", startRow = xmin, colNames = FALSE)
      addStyle(wb = wb, sheet = sheet, style = condi, cols = c("E", "F", "G", "I", "J", "K", "M", "N", "O"), rows = xmin + 1, gridExpand = TRUE, stack = TRUE)
      addStyle(wb = wb, sheet = sheet, style = f, cols = c("E", "F", "G", "I", "J", "K", "M", "N", "O"), rows = xmin, gridExpand = TRUE, stack = TRUE)
     
      writeData(wb = wb, sheet = sheet, x = data.frame("a" = c("R", as.character(df[x, "R"]))), startCol = "B", startRow = xmin + 2, colNames = FALSE)
      addStyle(wb = wb, sheet = sheet, style = Hpro, cols = "B", rows = xmin + 2, gridExpand = TRUE, stack = TRUE)
      addStyle(wb = wb, sheet = sheet, style = pro, cols = "B", rows = xmin + 3, gridExpand = TRUE, stack = TRUE)
     
      writeData(wb = wb, sheet = sheet, x = data.frame("a" = c("R", as.character(df[x, "R1"])), "b" = c("P", as.double(df[x, "P1"])), "c" = c("C", as.character(df[x, "C1"])), "d" = "",
                                                       "e" = c("R", as.character(df[x, "R2"])), "f" = c("P", as.double(df[x, "P2"])), "g" = c("C", as.character(df[x, "C2"])), "h" = "",
                                                       "i" = c("R", as.character(df[x, "R3"])), "j" = c("P", as.double(df[x, "P3"])), "k" = c("C", as.character(df[x, "C3"]))),
                startCol = "E", startRow = xmin + 2, colNames = FALSE)
     
      addStyle(wb = wb, sheet = sheet, style = a, rows = xmax - 1, cols = c("E", "F", "G", "I", "J", "K", "M", "N", "O"), gridExpand = TRUE, stack = TRUE)
      addStyle(wb = wb, sheet = sheet, style = r, rows = xmax, cols = c("E", "F", "G", "I", "J", "K", "M", "N", "O"), gridExpand = TRUE, stack = TRUE)
      addStyle(wb = wb, sheet = sheet, style = blue, rows = xmax + 1, cols = 1:15, gridExpand = TRUE, stack = TRUE)
      setRowHeights(wb = wb, sheet = sheet, rows = xmax + 1, heights = 5)
    }
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    > T1<-Sys.time()
    > end <- nrow(df)
    > sapply(1:end,mep)
    [[1]]
        1     2     7 
    "100"  "20"   "5" 
     
    [[2]]
        1     2     7    12 
    "100"  "20"   "5"   "5" 
     
    > T2<-Sys.time()
    > difftime(T2,T1)
    Time difference of 0.1715999 secs
    Cordialement,

  5. #5
    Membre averti
    Homme Profil pro
    Epitech
    Inscrit en
    Mai 2019
    Messages
    33
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 28
    Localisation : France, Bouches du Rhône (Provence Alpes Côte d'Azur)

    Informations professionnelles :
    Activité : Epitech

    Informations forums :
    Inscription : Mai 2019
    Messages : 33
    Par défaut
    Bonjour,

    Encore merci de votre retour, j'avais complètement oublié de passer par des variables pour stocker la formule, j'ai pu aussi améliorer le temps de traitement de cette manière je laisse une trace ca pourrait toujours servir à d'autres personnes. Donc en plus de modifier les formules par xmin et xmax, j'ai fait en sorte, comme je l'ai demandé précédemment, de pouvoir récupérer un vecteur pour les fonctions addStyle(). En effet en les mettant en dehors de la fonction et que la fonction sapply() les appellent plusieurs fois, je me suis dit qu'un seul appel de fonction devrais réduire le temps de traitement et en effet je suis passé de 16 secondes en moyennes lorsque les addStyle() étaient dans la fonction à 5 secondes en moyennes en dehors de la fonction

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
     
    my_v <- function(x, y) {
      if (x > end) {
        return (y)
      } else {
        xmin <- x + 2 + ((x - 1) * 4)
        xmax <- xmin + 3
        z <- c(y, xmin:xmax)
        return (my_v(x + 1, z))
      }
    }
     
    my_min <- function(x, y) {
      if (x > end) {
        return (y)
      } else {
        xmin <- x + 2 + ((x - 1) * 4)
        z <- c(y, xmin)
        return (my_min(x + 1, z))
      }
    }
     
    my_max <- function(x, y) {
      if (x > end) {
        return (y)
      } else {
        xmin <- x + 5 + ((x - 1) * 4)
        z <- c(y, xmin)
        return (my_max(x + 1, z))
      }
    }
     
    z <- numeric()
    end <- nrow(fam)
    xmxm <- my_v(1, z)
    mmin <- my_min(1, z)
    mmax <- my_max(1, z)
     
    addStyle(wb = wb, sheet = sheet, style = w, rows = xmxm, cols = 1:15, gridExpand = TRUE)
    addStyle(wb = wb, sheet = sheet, style = b, rows = xmxm, cols = 1, gridExpand = TRUE)
    addStyle(wb = wb, sheet = sheet, style = d, cols = 3, rows = mmin, stack = TRUE)
    addStyle(wb = wb, sheet = sheet, style = f, cols = c("E", "F", "G", "I", "J", "K", "M", "N", "O"), rows = mmin, gridExpand = TRUE, stack = TRUE)
    addStyle(wb = wb, sheet = sheet, style = c, cols = c("E", "F", "G", "I", "J", "K", "M", "N", "O"), rows = mmin + 1, gridExpand = TRUE, stack = TRUE)
    addStyle(wb = wb, sheet = sheet, style = Hpro, cols = "B", rows = mmin + 2, gridExpand = TRUE, stack = TRUE)
    addStyle(wb = wb, sheet = sheet, style = pro, cols = "B", rows = mmin + 3, gridExpand = TRUE, stack = TRUE)
    addStyle(wb = wb, sheet = sheet, style = a, rows = mmax - 1, cols = c("E", "F", "G", "I", "J", "K", "M", "N", "O"), gridExpand = TRUE, stack = TRUE)
    addStyle(wb = wb, sheet = sheet, style = r, rows = mmax, cols = c("E", "F", "G", "I", "J", "K", "M", "N", "O"), gridExpand = TRUE, stack = TRUE)
    addStyle(wb = wb, sheet = sheet, style = blue, rows = mmax + 1, cols = 1:15, gridExpand = TRUE, stack = TRUE)
    Cordialement,

    Arkning

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Optimisation temps de traitement
    Par Moriceot dans le forum VBA Access
    Réponses: 2
    Dernier message: 17/07/2017, 18h51
  2. Optimisation temps de traitement avec pandas (boucle)
    Par meuah dans le forum Général Python
    Réponses: 7
    Dernier message: 27/08/2015, 12h49
  3. Optimiser temps de traitement sur des requêtes simples
    Par yoyo88 dans le forum Langage SQL
    Réponses: 21
    Dernier message: 20/02/2015, 13h35
  4. Optimisation temps de traitement
    Par Alqualonde dans le forum Macro
    Réponses: 11
    Dernier message: 01/08/2012, 17h43
  5. [SQL] Optimisation temps de traitement PROC SQL
    Par amidujour dans le forum SAS Base
    Réponses: 2
    Dernier message: 13/10/2010, 21h16

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo